Skip to content

Commit 498a1d2

Browse files
committed
merge-two-sorted-lists
1 parent 36a3ba1 commit 498a1d2

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

merge-two-sorted-lists/jun0811.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
var mergeTwoLists = function (list1, list2) {
10+
if (!list1) return list2;
11+
else if (!list2) return list1;
12+
13+
if (list1.val <= list2.val) {
14+
list1.next = mergeTwoLists(list1.next, list2);
15+
return list1;
16+
} else {
17+
list2.next = mergeTwoLists(list1, list2.next);
18+
return list2;
19+
}
20+
};

0 commit comments

Comments
 (0)