Simple C Iterative Solution


  • 0
    S
    struct ListNode* reverseList(struct ListNode* head) {
       struct ListNode* currNode;
       struct ListNode* nextNode;
       struct ListNode* prevNode;
       
       if(head==NULL) return NULL;
    
       currNode = head;
       nextNode = head->next;
       prevNode = NULL;
    
    
       while(currNode) {
           nextNode = currNode->next;
           currNode->next = prevNode;
           prevNode = currNode;
           currNode = nextNode;
       }
    
       return prevNode;
    }

Log in to reply
 

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