C++ recursive solution.6ms


  • 0
    L
    ListNode* tail;
    ListNode* recurse(ListNode* head) {
    	if (head) {
    		ListNode* follow = recurse(head->next);
    		if (follow) follow->next = head;
    		else tail = head;
    	}
    	return head;
    }
    
    ListNode* reverseList(ListNode* head) {
    	if (head) {
    		recurse(head);
    		head->next = NULL;
    	}
    	return tail;
    }
    

Log in to reply
 

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