-
Notifications
You must be signed in to change notification settings - Fork 931
/
Copy pathlinked-list-2.js
130 lines (116 loc) · 2.98 KB
/
linked-list-2.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
const Node = require('./node');
/**
* Singly linked list with `last` element reference
*/
class LinkedList {
constructor() {
this.first = null; // head/root element
this.last = null;
}
/**
* Adds element to the end of the list (tail). Similar to Array.push
* Using the element last reference instead of finding last, we can reduce the runtime from O(n) to O(1)
* Runtime: O(1)
* @param {any} value
*/
addLast(value) {
const node = new Node(value);
if (this.first) {
const currentNode = this.first;
this.last.next = node;
this.last = node;
} else {
this.first = node;
this.last = node;
}
}
/**
* Removes element from the start of the list (head/root). similar Array.shift
* Runtime: O(1)
*/
removeFirst() {
const first = this.first;
if (first) {
this.first = first.next;
return first.value;
}
this.last = null;
}
/**
* Adds element to the begining of the list. Similar to Array.unshift
* Runtime: O(1)
* @param {any} value
*/
addFirst(value) {
const node = new Node(value);
node.next = this.first;
if (!this.first) {
this.last = node;
}
this.first = node;
}
/**
* Removes element to the end of the list
* similar to Array.pop
* Runtime: O(n)
*/
removeLast() {
let current = this.first;
let target;
if (current && current.next) {
while (current && current.next && current.next.next) {
current = current.next;
}
this.last = current;
target = current.next;
current.next = null;
} else {
this.first = null;
this.last = null;
target = current;
}
if (target) {
return target.value;
}
}
/**
* Find first occurence of the element matching the value
* return index or undefined
* Runtime: O(n)
* @param {any} value
*/
contains(value) {
for (let current = this.first, index = 0; current; index++, current = current.next) {
if (current.value === value) {
return index;
}
}
}
/**
* Remove the nth element from the list. Starting with 0
* Returns value if found or undefined if it was not found
* Runtime: O(n)
* @param {any} nth
*/
removeAt(nth) {
if (nth === 0) {
return this.removeFirst();
}
for (let current = this.first, index = 0; current; index++, current = current.next) {
if (index === nth) {
if (!current.next) { // if it doesn't have next it means that it is the last
return this.removeLast();
}
current.previous = current.next;
this.size--;
return current.value;
}
}
}
}
// Aliases
LinkedList.prototype.add = LinkedList.prototype.push = LinkedList.prototype.addLast;
LinkedList.prototype.remove = LinkedList.prototype.pop = LinkedList.prototype.removeLast;
LinkedList.prototype.unshift = LinkedList.prototype.addFirst;
LinkedList.prototype.shift = LinkedList.prototype.removeFirst;
module.exports = LinkedList;