-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path22-mergeKSortedLists.js
122 lines (105 loc) · 3 KB
/
22-mergeKSortedLists.js
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode[]} lists
* @return {ListNode}
*/
var mergeKLists = function (lists) {
if (lists.length === 0) {
return null;
}
let newList = [];
for (let i = 0; i < lists.length; i++) {
if (lists[i] !== null) {
newList.push(lists[i]);
}
}
if (newList.length === 0) {
return null;
}
let mergeSort = function (array) {
let merge = function (firstArray, secondArray) {
let i = 0;
let j = 0;
let result = [];
while (true) {
if (firstArray[i].val < secondArray[j].val) {
result.push(firstArray[i]);
i++;
} else {
result.push(secondArray[j]);
j++;
}
if (i === firstArray.length) {
return result.concat(
secondArray.slice(j, secondArray.length)
);
} else if (j === secondArray.length) {
return result.concat(
firstArray.slice(i, firstArray.length)
);
}
}
};
if (array.length === 1) {
return array;
}
const middleIndex = Math.floor(array.length / 2);
const firstArray = mergeSort(array.slice(0, middleIndex));
const secondArray = mergeSort(array.slice(middleIndex, array.length));
const merged = merge(firstArray, secondArray);
return merged;
};
let bubble = function (arr) {
let index = arr.length - 1;
while (true) {
if (index === 0) {
return arr;
}
if (arr[index].val < arr[index - 1].val) {
let temp = arr[index - 1];
arr[index - 1] = arr[index];
arr[index] = temp;
index--;
} else {
return arr;
}
}
};
let arr = mergeSort(newList);
console.log("sorted:");
console.log(arr);
let result = [];
while (arr.length > 0) {
result.push(arr[0]);
let nextNode = arr[0].next;
if (nextNode !== null) {
arr = bubble(arr.slice(1).concat([nextNode]));
} else {
arr = arr.slice(1);
}
}
for (let i = 0; i < result.length - 1; i++) {
result[i].next = result[i + 1];
}
return result[0];
};
function ListNode(val, next) {
this.val = val === undefined ? 0 : val;
this.next = next === undefined ? null : next;
}
g = new ListNode(5);
b = new ListNode(4, g);
a = new ListNode(1, b);
h = new ListNode(4);
d = new ListNode(3, h);
c = new ListNode(1, d);
i = new ListNode();
f = new ListNode(6);
e = new ListNode(2, f);
console.log(mergeKLists([a, c, e]));