Simple C++ iterative solution


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

Log in to reply
 

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