c++ iterative solution, use 3 pointer.


  • 0

    This is a very classic interview question, basic thought is use 3 pointers, prev, curr, next.

    class Solution {
    public:
      ListNode* reverseList(ListNode* head) {
        if (head == NULL || head -> next == NULL)
        {
          return head;
        }
    
        ListNode *prev = NULL;
        while (head != NULL)
        {
          ListNode *next = head -> next;
          head -> next = prev;
          prev = head;
          head = next;
        }
        return prev;
      }
    };
    

Log in to reply
 

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