-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedList.js
216 lines (196 loc) · 4.92 KB
/
linkedList.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
class Node {
//node we have data, and next for reference to next act as pointer property
constructor(data) {
this.data = data;
this.next = null;
}
}
// const n1 = new Node(200);
// console.log(n1);
class LinkedList {
constructor() {
this._head = null;
this.size = 0;
}
insert(data) {
const newNode = new Node(data); //creates a new node .
if (!this._head) {
this._head = newNode; //check if linked list is empty, if empty insert node .
} else {
//if not empty, traverse the node and insert the last node
let current = this._head;
while (current.next) {
current = current.next;
}
current.next = newNode;
this.size++;
}
}
addFirst(data) {
let newNode = new Node(data);
if (!this._head) {
this._head = newNode;
} else {
let current = this._head;
newNode = current;
this._head = newNode;
}
this.size++;
}
delete(data) {
let previousNode;
if (!this._head) {
//if there is no head ,return null
return null;
}
if (this._head.data === data) {
//checks if head contains data to be deleted, if yes
//it sets to the next node ,removing it
this._head = this._head.next;
return;
}
let current = this._head;
while (current.data !== data) {
//if current Node does not contain data thats needed ,set previous node equal to current
previousNode = current;
current = current.next;
previousNode.next = current.next;
}
this.size--;
}
removeduplicate() {
let current = this._head;
while (current && current.next) {
if (current.data === current.next.data) {
// compares current node and next current node
current.next = current.next.next; // removes duplicates if curent node is equal to the next current node
this.size--; //We also decrement the size of the linked list since we are removing a node.
} else {
current = current.next;
}
}
return this._head;
}
getAt(index) {
let current = this._head;
let count = 0;
while (current) {
if (count == index) {
console.log(current.data, "heelo");
}
current++;
current = current.next;
}
}
clear() {
this._head = null;
}
getMiddleNode() {
//using fast and slow pointer
let fast = this._head;
let slow = this._head;
if (this._head != null) {
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
console.log(slow.data);
return slow.data;
}
}
getLastNode() {
let lastNode = this._head;
if (lastNode) {
while (lastNode.next) {
lastNode = lastNode.next;
console.log(lastNode.next);
}
}
return lastNode;
}
oddEvenList() {
if (!this._head) return null;
let even = this._head.next; // 1,2,3,4,5,6 so even are 2,4,6
let odd = this._head;
let evenHead = even; // pointer of beginning of even list
while (even && even.next) {
odd.next = even.next; // Connect odd to next even node
odd = odd.next;
even.next = odd.next; // Connect even to next odd node
even = even.next;
}
odd.next = evenHead;
return this._head;
}
//alternative
oddEvenLists(head) {
if (!head) return head;
var odd = head;
var even = head.next;
while (odd.next && odd.next.next) {
var tmp = odd.next; //storing odd into temp
odd.next = odd.next.next;
odd = odd.next;
tmp.next = odd.next;
}
odd.next = even; //swapping temp of old values with even
return head;
}
reverse() {
let prev = null;
let current = this._head;
while (current !== null) {
let next = current.next;
current.next=prev
prev = current;
current = next;
}
this._head = prev;
}
removeElements = (val) => {
if (!this._head) return null;
let allsame = true;
let curr = head;
while (curr) {
if (curr.val != val) {
allsame = false;
break;
}
curr = curr.next;
}
if (allsame) return null;
// Remove matching values from the beginning of the list
while (this._head && this._head.val === val) {
//check if value exist in a node
this._head = this._head.next;
}
// Remove matching values from the rest of the list
let previousNode = this._head;
let current = this._head;
while (current) {
if (current.val === val) {
previousNode.next = current.next;
} else {
previousNode = current;
}
current = current.next;
}
return this._head;
};
}
const list = new LinkedList();
//list.addFirst(9);
list.insert(1);
//list.insert(1);
list.insert(2);
list.insert(3);
list.insert(4);
list.insert(5);
// list.delete(9);
// list.getAt(2);
//list.removeduplicate();
// list.getLastNode();
// list.clear()
list.reverse()
//list.getMiddleNode();
console.log(list._head);