Skip to content

Commit 7fe5cca

Browse files
committed
Create single-linked-node.js
1 parent 515b41d commit 7fe5cca

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

single-linked-node.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
var SinglyLinkedList = function(){}
2+
3+
SinglyLinkedList.prototype = {
4+
addBottom: function(node) {
5+
if (this.head == undefined) return this.head = node;
6+
var currentNode = this.head;
7+
while(currentNode.next !== undefined) {
8+
currentNode = currentNode.next;
9+
}
10+
currentNode.next = node;
11+
},
12+
13+
find: function(data) {
14+
var currentNode = this.head;
15+
while(currentNode !== undefined) {
16+
if(currentNode.data == data) return currentNode;
17+
currentNode = currentNode.next;
18+
}
19+
},
20+
21+
addTop: function(node) {
22+
if (this.head == undefined) return this.head = node;
23+
node.next = this.head;
24+
this.head = node;
25+
},
26+
27+
remove: function(data) {
28+
if (this.head.data == data) return this.head = this.head.next;
29+
var prevNode = this.head;
30+
var currentNode = this.head.next;
31+
while(currentNode !== undefined) {
32+
if(currentNode.data == data) {
33+
prevNode.next = currentNode.next;
34+
return currentNode.next = undefined;
35+
}
36+
prevNode = currentNode;
37+
currentNode = currentNode.next;
38+
}
39+
}
40+
}
41+
42+
module.exports = SinglyLinkedList;

0 commit comments

Comments
 (0)