My C++ Accepted recursive simple answer


  • 1
    A
    class Solution {
        ListNode* tail;
    
    public:
        ListNode* reverseList(ListNode* head) {
    
           if(!head)
            return head;
    
            reverseList(head, NULL);
            return tail;
        }
        
         void reverseList(ListNode* head, ListNode* prev) 
         {
            if(!head)
            {
                tail = prev;
                return;
            }
    
            reverseList(head->next, head);
            head->next =  prev;
         }
    };

Log in to reply
 

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