Skip to content

Instantly share code, notes, and snippets.

@TechWithTy
Created October 27, 2020 00:55
Show Gist options
  • Save TechWithTy/73ec6be3dc36f6cbae3d446978309555 to your computer and use it in GitHub Desktop.
Save TechWithTy/73ec6be3dc36f6cbae3d446978309555 to your computer and use it in GitHub Desktop.
Merge Two sorted Linked lists
let mergeTwoLists = function (l1, l2) {
let dummy = new ListNode(-1);
let head = dummy;
while (l1 !== null && l2 !== null) {
if (l1.val <= l2.val) {
dummy.next = l1;
l1 = l1.next;
} else {
dummy.next = l2;
l2 = l2.next;
}
dummy = dummy.next;
}
if (l1 !== null) {
dummy.next = l1;
} else {
dummy.next = l2;
}
return head.next;
};
class ListNode {
constructor(val = null, next = null) {
this.val = val;
this.next = next;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment