time O(n) space O(1) Recurssion


  • 0
    X
    public ListNode reverseList(ListNode head) {
        // base case
        if(null == head || null == head.next)
            return head;
        ListNode rest = head.next; // rest of the linked list
        // reversed rest linked list
        ListNode reversedRest = reverseList(rest); 
        rest.next = head; // connect head and rest
        head.next = null;
        return reversedRest;
    }
    

Log in to reply
 

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