-
Notifications
You must be signed in to change notification settings - Fork 21
/
mergeKSortedLists.js
155 lines (136 loc) · 2.89 KB
/
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
Input:
1>4>5
1>3>4
2>6
Output: 1>1>2>3>4>4>5>6
*/
function bruteForceMergeList(lists) {
const linkedLists = Object.values(lists);
let array = [];
linkedLists.forEach(list => {
if(!list) return;
let ptr = list.head;
while(ptr) {
array.push(ptr.val);
ptr = ptr.next;
}
});
array.sort();
return array;
function constructLinkedListFromArray(array){
let newList = new LinkedList();
let currentPtr = newList.head;
array.forEach( element => {
let node = new Node(element);
if(!newList.head) {
newList.head = node;
currentPtr = node;
}
else {
currentPtr.next = node;
currentPtr = currentPtr.next;
}
});
return newList;
}
}
function mergeHelper(listA, listB) {
let pointerA, pointerB;
pointerA = listA.head;
pointerB = listB.head;
while(pointerA.next || pointerB.next) {
while (pointerA.val < pointerB.val) {
pointerA = pointerA.next;
}
if (pointerA.next.val > pointerB.val) {
let currentB = pointerB;
console.log(pointerA);
console.log(pointerB);
pointerA.next = currentB;
currentB.next = pointerA;
pointerB = pointerB.next;
}
else {
listA.tail = listB.head;
}
// console.log(listA);
// console.log(listB);
}
return listA;
}
class Node {
constructor(val){
this.val = val;
this.next = null;
}
}
class LinkedList{
constructor(){
this.head = null;
this.tail = null;
this.length = 0;
}
push(value){
const newNode = new Node(value);
if(!this.head) {
this.head = newNode;
this.tail = newNode;
} else {
this.tail.next = newNode;
this.tail = newNode;
}
this.length++;
return this;
}
pop(){
if (!this.head) return undefined;
let current = this.head;
let newTail= current;
while (current.next) {
newTail = current;
current = current.next;
}
this.tail = newTail;
this.tail.next = null;
this.length--;
if (this.length === 0) {
this.head = null;
this.tail = null;
}
return current;
}
}
const list1 = new LinkedList();
list1.push(1);
list1.push(4);
list1.push(5);
const list2 = new LinkedList();
list2.push(1);
list2.push(3);
list2.push(4);
const list3 = new LinkedList();
list2.push(2);
list2.push(6);
const lists = {
list1,
list2,
list3
};
console.log(bruteForceMergeList(lists));
// console.log(mergeHelper(list1, list2));
/*
a,c
1 ->
2-> 2 -> 4
3
b, c
loop as long as pointerA is less than pointerB, increment pointerA
if pointer A is greater or equal to pointerB
pointerB should be detached ->
let currentA = pointerA
currentB = pointerB
currentB = currentA
pointerB.next = pointerA
*/