Simplest iterative C++ code in O(n) .


  • 0
    R


    void func(ListNode** head)
    {
    ListNode *prev=NULL, *current=*head, *nextnode=NULL;

    while(current!=NULL)
    {
        nextnode=current->next;
        
        current->next=prev;
        prev=current;
        current=nextnode;
        
    }
    *head=prev;
    

    }
    class Solution {
    public:
    ListNode* reverseList(ListNode* head) {

      func(&head);
      return head;
    }
    

    };


Log in to reply
 

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