Created
January 3, 2019 05:24
-
-
Save j-quelly/9a1f69cf504e178dfbf6f67d9678b4b0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Definition for singly-linked list: | |
// function ListNode(x) { | |
// this.value = x; | |
// this.next = null; | |
// } | |
// | |
function mergeTwoLinkedLists(l1, l2) { | |
const list = []; | |
let left = l1; | |
let right = l2; | |
while (left || right) { | |
if (left && right) { | |
if (left.value < right.value) { | |
list.push(left.value); | |
left = left && left.next; | |
} else { | |
list.push(right.value); | |
right = right && right.next; | |
} | |
} else if (left && !right) { | |
list.push(left.value); | |
left = left && left.next; | |
} else if (right && !left) { | |
list.push(right.value); | |
right = right && right.next; | |
} | |
} | |
return list; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment