My iterative solution in C++


  • 0
    A

    ListNode* reverseList(ListNode*& head) {
    ListNode* curr=head;
    if(!head)
    return NULL;
    if(!curr->next)
    return curr;
    ListNode* prev=NULL;
    while(curr)
    {
    ListNode* next=curr->next;
    curr->next=prev;
    prev=curr;
    curr=next;
    }
    head=prev;
    return head;
    }


Log in to reply
 

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