Ye.
Ye.

Reputation: 181

Javascript Linked List merge returning undefined

Trying to merge two sorted list question from LeetCode, keep running into undefined result:

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} l1
 * @param {ListNode} l2
 * @return {ListNode}
 */
var mergeTwoLists = function(l1, l2) {
    var res = new ListNode();
    var curr = res;
    while(l1 !== null && l2 !== null) {
        if(l1.val <= l2.val) {
            // Set current node to l1 if less than or equal
            curr = l1;
            // Move l1's head to next
            l1 = l1.next
        } else {
            // Else same case for l2
            curr = l2;
            l2 = l2.next;
        }
        // Move current to next
        curr = curr.next
    }

    if (l1 !== null) {
        curr = l1;
    } else if (l2 !== null) {
        curr = l2;
    }

    return res;
};

Not sure why res is returning undefined. From what I can tell, I'm setting res to be the head pointer for the resulting linked list nodes and performing the following logic:

While L1 or L2 has not reached its end, compare values.
IF L1.val is less than or equal to L2.val, set it to the current node, then bump up the pointer for L1 to the next value.
ELSE do the mirror for L2.

Once the current node value is set, bump up the node to the next item (either L1.next or L2.next, which will be overridden by the next larger value from either of the lists) and repeat.

Once we reach the end of either L1 or L2, set the remaining linked list to the current node, then return the head res pointer of the full list.

Not sure where I'm going wrong here with the logic, maybe it's getting a little late haha. Apologies if this has been repeated, appreciate any help!

Upvotes: 0

Views: 1962

Answers (2)

shawon191
shawon191

Reputation: 1955

When you are doing current = res it updates the reference current is holding from res to l1/l2. To avoid this, what you want to do is to set the next node of current instead of the variable current itself. The following code works -

var mergeTwoLists = function(l1, l2) {
    var res = new ListNode();
    var curr = res;
    while(l1 !== null && l2 !== null) {
        if(l1.val <= l2.val) {
            // Set current node to l1 if less than or equal
            curr.next = l1;
            // Move l1's head to next
            l1 = l1.next
        } else {
            // Else same case for l2
            curr.next = l2;
            l2 = l2.next;
        }
        // Move current to next
        curr = curr.next
    }

    if (l1 !== null) {
        curr.next = l1;
    } else if (l2 !== null) {
        curr.next = l2;
    }

    return res.next;
};

Upvotes: 2

Zitzabis
Zitzabis

Reputation: 178

I could be wrong but it looks like you're making a copy of res into curr. Unless you're doing it by reference, it should treat those separately. Meaning all changes you're making to curr are not being reflected on res.

Upvotes: 1

Related Questions