-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmergeTwoSortedLists.js
More file actions
46 lines (38 loc) · 984 Bytes
/
mergeTwoSortedLists.js
File metadata and controls
46 lines (38 loc) · 984 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//Definition for singly-linked list.
function ListNode(val, next) {
this.val = (val===undefined ? 0 : val)
this.next = (next===undefined ? null : next)
}
/**
* @param {ListNode} list1
* @param {ListNode} list2
* @return {ListNode}
*/
var mergeTwoLists = function(list1, list2) {
//check if one of the lists is undefined
if(!list1 || !list2){
if(list1) return list1;
else return list2;
}
return recursion(list1,list2)
};
function recursion (node1, node2) {
//both nodes are undefined
if(!node1 && !node2){
return undefined;
}
//node 1 is undefined
if(!node1){
return new ListNode(node2.val, recursion(node1,node2.next));
}
//node 2 is undefined
if(!node2) {
return new ListNode(node1.val, recursion(node1.next,node2));
}
//none is undefined
if(node1.val < node2.val){
return new ListNode(node1.val, recursion(node1.next, node2));
}else {
return new ListNode(node2.val, recursion(node1, node2.next));
}
}