Skip to content

Commit cfffaa3

Browse files
committed
feat: add solution of Merge Two Sorted Lists(021) with javascript
1 parent 5535446 commit cfffaa3

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/_021/Solution.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val) {
4+
* this.val = val;
5+
* this.next = null;
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} l1
10+
* @param {ListNode} l2
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

Comments
 (0)