8mm simple C++ recursive solution


  • 1
    M
    ListNode* reverseList(ListNode* head) {
            if(head==NULL||head->next==NULL)
                return head;
            
            if(head->next->next == NULL){
                ListNode* tmp = head->next;
                tmp->next = head;
                head->next = NULL;
                return tmp;
            }
            
            ListNode* newhead = reverseList(head->next);
            head->next->next = head;
            head->next = NULL;
            return newhead;
        }

Log in to reply
 

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