8ms C iterative code


  • 0
    L

    Iterative code to reverse a link list...It took 8ms. Please provide any input to improve this further.

    void reverse_list(ListNode *list,ListNode **head)
        {
            if(list->next==NULL)
            {
                *head = list;
                return;
            }
        
            reverse_list(list->next,head);
            list->next->next = list;
            list->next = NULL;
        }
        
        ListNode* reverseList(ListNode* head) {
            
            if((head==NULL) || (head->next==NULL)) 
                return head;
            
            ListNode *reverse_head=NULL;
            reverse_list(head,&reverse_head);
            return reverse_head;
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.