Created
October 27, 2020 00:55
-
-
Save TechWithTy/73ec6be3dc36f6cbae3d446978309555 to your computer and use it in GitHub Desktop.
Merge Two sorted Linked lists
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
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