Skip to content

Commit 56ec0be

Browse files
committed
Merge K Sorted Lists solution
1 parent 28ce67b commit 56ec0be

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

22-mergeKSortedLists.js

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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[]} lists
10+
* @return {ListNode}
11+
*/
12+
var mergeKLists = function (lists) {
13+
if (lists.length === 0) {
14+
return null;
15+
}
16+
let newList = [];
17+
18+
for (let i = 0; i < lists.length; i++) {
19+
if (lists[i] !== null) {
20+
newList.push(lists[i]);
21+
}
22+
}
23+
24+
if (newList.length === 0) {
25+
return null;
26+
}
27+
28+
let mergeSort = function (array) {
29+
let merge = function (firstArray, secondArray) {
30+
let i = 0;
31+
let j = 0;
32+
let result = [];
33+
while (true) {
34+
if (firstArray[i].val < secondArray[j].val) {
35+
result.push(firstArray[i]);
36+
i++;
37+
} else {
38+
result.push(secondArray[j]);
39+
j++;
40+
}
41+
42+
if (i === firstArray.length) {
43+
return result.concat(
44+
secondArray.slice(j, secondArray.length)
45+
);
46+
} else if (j === secondArray.length) {
47+
return result.concat(
48+
firstArray.slice(i, firstArray.length)
49+
);
50+
}
51+
}
52+
};
53+
54+
if (array.length === 1) {
55+
return array;
56+
}
57+
const middleIndex = Math.floor(array.length / 2);
58+
const firstArray = mergeSort(array.slice(0, middleIndex));
59+
const secondArray = mergeSort(array.slice(middleIndex, array.length));
60+
const merged = merge(firstArray, secondArray);
61+
return merged;
62+
};
63+
64+
let bubble = function (arr) {
65+
let index = arr.length - 1;
66+
67+
while (true) {
68+
if (index === 0) {
69+
return arr;
70+
}
71+
if (arr[index].val < arr[index - 1].val) {
72+
let temp = arr[index - 1];
73+
arr[index - 1] = arr[index];
74+
arr[index] = temp;
75+
76+
index--;
77+
} else {
78+
return arr;
79+
}
80+
}
81+
};
82+
83+
let arr = mergeSort(newList);
84+
console.log("sorted:");
85+
console.log(arr);
86+
let result = [];
87+
88+
while (arr.length > 0) {
89+
result.push(arr[0]);
90+
let nextNode = arr[0].next;
91+
if (nextNode !== null) {
92+
arr = bubble(arr.slice(1).concat([nextNode]));
93+
} else {
94+
arr = arr.slice(1);
95+
}
96+
}
97+
98+
for (let i = 0; i < result.length - 1; i++) {
99+
result[i].next = result[i + 1];
100+
}
101+
102+
return result[0];
103+
};
104+
105+
function ListNode(val, next) {
106+
this.val = val === undefined ? 0 : val;
107+
this.next = next === undefined ? null : next;
108+
}
109+
110+
g = new ListNode(5);
111+
b = new ListNode(4, g);
112+
a = new ListNode(1, b);
113+
114+
h = new ListNode(4);
115+
d = new ListNode(3, h);
116+
c = new ListNode(1, d);
117+
118+
i = new ListNode();
119+
f = new ListNode(6);
120+
e = new ListNode(2, f);
121+
122+
console.log(mergeKLists([a, c, e]));

0 commit comments

Comments
 (0)