Tripped out by this!


  • 0
    A

    Hey guys,

    I've been fairly decent at getting through these algorithm problems so far but something has been tripping me up - that's the code below:

    var reverseList = function(head) {
        
        if (!head || !head.next) {
            return head;
            }
            
            
        var newHead = reverseList(head.next);
        
        head.next.next = head;
        head.next = null;
        return newHead;
        };
    

    How does modifying head with the head.next.next = head and the head.next = null affect the return value aka newHead? I know it's an essential step but I can't figure out why changing that affects the return value. Thanks in advance!


Log in to reply
 

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