-
Notifications
You must be signed in to change notification settings - Fork 0
/
data-struc-linked-list.js
39 lines (36 loc) · 1.09 KB
/
data-struc-linked-list.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
//https://www.youtube.com/watch?v=9YddVVsdG5A
function LinkedList () {
this.head = null;
this.length = 0;
this.Node = function (element) {
this.element = element;
this.next = null
}
}
LinkedList.prototype.add = function(elem) {
let node = new this.Node(elem); // create new node
if (this.head == null) { // if head is null, then new node is head
this.head = node;
} else {
var currentNode = head; // save current pointer
while (currentNode.next) { // iterate next node till you reach end of node.
currentNode = currentNode.next;
}
currentNode.next = node; // point new node to last node's next
}
this.length++;
}
LinkedList.prototype.remove = function(elem) {
var currentNode = this.head;
var prevNode = null
if (currentNode.element === elem) {
this.head = currentNode.next;
} else {
while (currentNode.element !== elem) {
prevNode = currentNode;
currentNode = currentNode.next;
}
prevNode.next = currentNode.next;
}
this.length--
}