forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Linked_List.js
95 lines (95 loc) · 2.15 KB
/
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
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
class linkedlist
{
constructor(value)
{
this.head={ //defining a linked list(access) O(1)
value:value,
next:null
}
this.tail=this.head;
this.length=1;
}
append(value) //function to add a node at the end of a existing node O(1)
{
const newNode={
value:value,
next:null
};
this.tail.next=newNode;
this.tail=newNode;
this.length++;
return this;
}
prepend(value) //adding a node to beginning of a linked list //o(1)
{
const newNode={
value:value,
next:null
}
newNode.next=this.head;
this.head=newNode;
this.length++
return this;
}
printlist() //printing entire list in form of an array
{
const array=[];
let currentNode=this.head;
while(currentNode!==null)
{
array.push(currentNode.value);
currentNode=currentNode.next;
}
return array;
}
insert(index,value) //O(n) time as it involves traversing
{
if(index>=this.length)
{
return this.append(value) //we can do anything in this step such as throwing error or any other althernative
}
const newNode={
value:value, //defining new node to be inserted
next:null
};
const leader=this.traverseToIndex(index-1);
const holdingpointer=leader.next;
leader.next=newNode;
newNode.next=holdingpointer;
this.length++;
return this.printlist;
}
traverseToIndex(index)
{
let counter=0;
let currentNode=this.head;
while(counter!==index)
{
currentNode=currentNode.next;
counter++;
}
return currentNode;
}
//remove from starting index O(1)
removestarting()
{
const tempNode=this.head;
this.head=this.head.next;
delete tempNode.value,tempNode.next;
}
remove(index) //O(n) time as it involves traversing
{
//check parameters if you want
const leader=this.traverseToIndex(index-1);
const unwantedNode=leader.next;
leader.next=unwantedNode.next;
this.length--;
return this.printlist();
}
}
const mylinkedlist=new linkedlist(10);
mylinkedlist.append(20);
mylinkedlist.append(30);
mylinkedlist.prepend(5);
mylinkedlist.printlist();
//console.log(mylinkedlist);