What is wrong in my code? Please help!!


  • 0
    S
    struct ListNode* reverseList(struct ListNode* head) {
        if(head->next==NULL)
        return head;
        struct ListNode *prev,*nex,*ptr;
        prev=NULL;
        ptr=head->next;
        while(ptr!=NULL)
        {
            nex=ptr->next;
            ptr->next=prev;
            prev=ptr;
            ptr=nex;
        }
        head->next=prev;
        return head;
    }

  • 0
    E

    In line above your return:

    head->next=prev;
    

    This turns the list into a loop. Instead, return prev, which is the new starting node.

    Also, add in:

    head->next=NULL;
    

    This is the end of your reversed list, which should terminate in NULL.


Log in to reply
 

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