-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmerge-two-sorted-linked-lists.js
More file actions
executable file
·82 lines (62 loc) · 1.55 KB
/
merge-two-sorted-linked-lists.js
File metadata and controls
executable file
·82 lines (62 loc) · 1.55 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*https://leetcode.com/problems/merge-two-sorted-lists/description/
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
Example:
Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4
*/
// Definition for singly-linked list.
function ListNode(val) {
this.val = val;
this.next = null;
}
/*Break the list into 2 nodes of the ListNode class. Then execute the sort function on the values of the list elements, and make a new list. Return the head of the new list */
// My Solution
var mergeTwoLists = function(l1, l2) {
// Make a new list, being an empty array
var result = [];
while (l1) {
result.push(new ListNode(l1.val));
l1 = l1.next;
}
while (l2) {
result.push(new ListNode(l2.val));
l2 = l2.next;
}
result.sort(function(a, b) {
return a.val - b.val;
});
if(!result.length) return null;
for(var i = 0; i < result.length -1; i++)
result[i].next = result[i+1];
return result[0];
}
// Alternative Solution - Here also I break the list into 2 nodes and then create a new a new list.
var mergeTwoLists = function(l1, l2) {
if(!l1) return l2;
if(!l2) return l1;
var head = null;
if(l1.val < l2.val) {
head = l1;
l1 = l1.next;
} else {
head = l2;
l2 = l2.next;
}
var newList = head;
while(l1 && l2) {
if(l1.val < l2.val) {
newList.next = l1;
l1 = l1.next;
} else {
newList.next = l2;
l2 = l2.next;
}
newList = newList.next;
}
if(!l1) {
newList.next = l2;
} else {
newList.next = l1;
}
return head;
}