- Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathsingle-linked-node.js
More file actions
Latest commit
42 lines (37 loc) · 1.08 KB
/
Copy pathsingle-linked-node.js
File metadata and controls
42 lines (37 loc) · 1.08 KB
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
varSinglyLinkedList=function(){}
SinglyLinkedList.prototype={
addBottom: function(node){
if(this.head==undefined)returnthis.head=node;
varcurrentNode=this.head;
while(currentNode.next!==undefined){
currentNode=currentNode.next;
}
currentNode.next=node;
},
find: function(data){
varcurrentNode=this.head;
while(currentNode!==undefined){
if(currentNode.data==data)returncurrentNode;
currentNode=currentNode.next;
}
},
addTop: function(node){
if(this.head==undefined)returnthis.head=node;
node.next=this.head;
this.head=node;
},
remove: function(data){
if(this.head.data==data)returnthis.head=this.head.next;
varprevNode=this.head;
varcurrentNode=this.head.next;
while(currentNode!==undefined){
if(currentNode.data==data){
prevNode.next=currentNode.next;
returncurrentNode.next=undefined;
}
prevNode=currentNode;
currentNode=currentNode.next;
}
}
}
module.exports=SinglyLinkedList;