Slightly different C++ version at 6ms


  • 0
    S
    class Solution {
        
    public:
         ListNode* rev(ListNode* head, ListNode*& tail) {
            if(head == NULL || head->next == NULL) {
                tail = head;
                return head;
            }
            
            (rev(head->next, tail))->next = head;
            head->next = NULL;
            return head;
            
        }
        ListNode* reverseList(ListNode* head) {
            ListNode* tail = NULL;
            rev(head, tail);
            return tail;
        }
    };
    

Log in to reply
 

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