We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent cb581a8 commit d1ba018Copy full SHA for d1ba018
0021-merge-two-sorted-lists/0021-merge-two-sorted-lists.js
@@ -0,0 +1,29 @@
1
+/**
2
+ * Definition for singly-linked list.
3
+ * function ListNode(val, next) {
4
+ * this.val = (val===undefined ? 0 : val)
5
+ * this.next = (next===undefined ? null : next)
6
+ * }
7
+ */
8
9
+ * @param {ListNode} list1
10
+ * @param {ListNode} list2
11
+ * @return {ListNode}
12
13
+var mergeTwoLists = function(l1, l2) {
14
+ var mergedHead = { val : -1, next : null },
15
+ crt = mergedHead;
16
+ while(l1 && l2) {
17
+ if(l1.val > l2.val) {
18
+ crt.next = l2;
19
+ l2 = l2.next;
20
+ } else {
21
+ crt.next = l1;
22
+ l1 = l1.next;
23
+ }
24
+ crt = crt.next;
25
26
+ crt.next = l1 || l2;
27
+
28
+ return mergedHead.next;
29
+};
0 commit comments