Easy solution in C++


  • 0
    W
    class Solution {
    public:
        ListNode* reverseList(ListNode* head) {
            ListNode * node = head;
            ListNode * result = NULL;
            ListNode * p = NULL;
            
            while( node != NULL )
            {
                p = node->next;    // save the next node's address
                node->next = result;
                result = node;
                node = p;
            }
            
            return result;
        }
    };

Log in to reply
 

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