Search code examples
javascriptvariablesscopeglobal-variables

My solution to a LeetCode problem works fine, until I try to improve it by using a helper function. The argument isn't being returned


Hey and thanks in advance for the help guys!

So I seem to be having a problem implementing a helper function while trying to solve a LeetCode problem. I've already found a working solution, but when I try to wrap repeated logic in a helper function, it seems the argument isn't being returned.

The problem is to merge two sorted linked lists and return it as a new sorted list. The new list should be made by splicing together the nodes of the first two lists. Here is a link to the problem on LC: https://leetcode.com/problems/merge-two-sorted-lists/

This is my working solution:

var mergeTwoLists = function(l1, l2) {
    let dummyHead = new ListNode(0),
        q = l1,
        v = l2,
        curr = dummyHead;
    while (q !== null || v !== null) {
        if (q && v) {
            if ( q.val !== null && v.val !== null) {
                if (q.val <= v.val) {
                    curr.next = new ListNode(q.val);
                    curr = curr.next;
                    q = q.next;        
                } else {
                    curr.next = new ListNode(v.val);
                    curr = curr.next;
                    v = v.next;
                }
            } else if (q.val !== null) {
                curr.next = new ListNode(q.val);
                curr = curr.next;
                q = q.next;
            } else {
                curr.next = new ListNode(v.val);
                curr = curr.next;
                v = v.next;
            }
        } else if (q) {
            curr.next = new ListNode(q.val);
            curr = curr.next;
            q = q.next;
        } else {
            curr.next = new ListNode(v.val);
            curr = curr.next;
            v = v.next;
        }      
    }
    return dummyHead.next;
};

So as you can see, the same step is repeated multiple times throughout my solution. Once I tried to handle it with a helper function, it seems there's an issue with the argument curr being returned. It always appears fresh each time the function is run by the algorithm.

I'm sure I'm missing something obvious. Any help would be appreciated :) Here is the solution implemented with a helper function:

var mergeTwoLists = function(l1, l2) {
    let dummyHead = new ListNode(0),
        q = l1,
        v = l2,
        curr = dummyHead;
    while (q !== null || v !== null) {
        if (q && v) {
            if ( q.val !== null && v.val !== null) {
                if (q.val <= v.val) {
                    attachNode(curr, q);
                } else {
                    attachNode(curr, v);
                }
            } else if (q.val !== null) {
                attachNode(curr, q);
            } else {
                attachNode(curr, v);
            }
        } else if (q) {
            attachNode(curr, q);
        } else {
            attachNode(curr, v);
        }    
    }
    return dummyHead.next;
};

function attachNode(curr, value) {
    curr.next = new ListNode(value.val);
    curr = curr.next;
    value = value.next;
    return curr;
}

Solution

  • You're making a mistake with function scope. If you have a function like

    function f(arg) {
        arg = 'another value';
    }
    

    then arg in the calling scope does not change. The parameter arg is a copy of the argument in the calling scope whose value does not change the argument in the calling scope:

    let arg = 'some value';
    f(arg);
    arg === 'some value'; // this is true
    

    It looks like you are considering this to some extent because you return cur from the function but you don't assign the value to anything.