Skip to content

Commit 99b8d63

Browse files
committed
Linked List in js
1 parent 7d234ef commit 99b8d63

File tree

1 file changed

+226
-0
lines changed

1 file changed

+226
-0
lines changed

Linked List/linked_list.js

+226
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
/*
2+
insertAtTail (val)
3+
insertAtHead (val)
4+
deleteAtTail ()
5+
deleteAtHead ()
6+
getLength ()
7+
print ()
8+
getAtIdx (idx)
9+
putAtIdx (val, idx)
10+
deleteAtIdx (idx)
11+
search (val)
12+
toArray ()
13+
reverse ()
14+
*/
15+
16+
class Node {
17+
constructor(val) {
18+
this.val = val
19+
this.next = null
20+
}
21+
}
22+
23+
class LinkedList {
24+
constructor() {
25+
this.head = null
26+
this.tail = null
27+
this.length = 0
28+
}
29+
30+
insertAtTail(val) {
31+
const node = new Node(val)
32+
33+
if (!this.head) {
34+
this.head = node
35+
this.tail = node
36+
this.length++
37+
} else {
38+
this.tail.next = node
39+
this.tail = node
40+
this.length++
41+
}
42+
}
43+
44+
insertAtHead(val) {
45+
const node = new Node(val)
46+
47+
if (!this.head) {
48+
this.head = node
49+
this.tail = node
50+
this.length++
51+
} else {
52+
node.next = this.head
53+
this.head = node
54+
this.length++
55+
}
56+
}
57+
58+
print() {
59+
if (this.length === 0) console.log ("Empty Linked List!")
60+
else {
61+
let curr = this.head
62+
let str = ''
63+
64+
while (curr) {
65+
str += curr.val + ' -> '
66+
curr = curr.next
67+
}
68+
69+
console.log(str + 'null')
70+
}
71+
}
72+
73+
deleteAtTail() {
74+
if (!this.head) console.log("Empty Linked List!")
75+
76+
else {
77+
let curr = this.head
78+
let prev = this.head
79+
80+
while (curr.next) {
81+
prev = curr
82+
curr = curr.next
83+
}
84+
85+
prev.next = null
86+
this.tail = prev
87+
this.length--
88+
}
89+
}
90+
91+
deleteAtHead() {
92+
if (!this.head) console.log("Empty Linked List!")
93+
94+
else {
95+
let curr = this.head
96+
this.head = curr.next
97+
this.length--
98+
}
99+
}
100+
101+
102+
getLength() {
103+
return this.length
104+
}
105+
106+
getAtIdx (idx) {
107+
if (idx >= this.length || idx < 0) throw new Error ("Index out of bounds")
108+
109+
else {
110+
let currIdx = 0
111+
let curr = this.head
112+
113+
while (currIdx < idx) {
114+
currIdx++
115+
curr = curr.next
116+
}
117+
118+
return curr.val
119+
}
120+
}
121+
122+
putAtIdx (val, idx) {
123+
if (idx > this.length || idx < 0) throw new Error ("Index out of bounds")
124+
125+
const node = new Node (val)
126+
127+
if (!this.head) {
128+
this.head = node
129+
this.length++
130+
return
131+
}
132+
133+
let currIdx = 0
134+
let curr = this.head
135+
let prev = this.head
136+
137+
while (currIdx < idx) {
138+
currIdx++
139+
prev = curr
140+
curr = curr.next
141+
}
142+
143+
prev.next = node
144+
node.next = curr
145+
this.length++
146+
}
147+
148+
deleteAtIdx (idx) {
149+
if (idx >= this.length || idx < 0) throw new Error ("Index out of bounds")
150+
151+
else {
152+
let currIdx = 0
153+
let curr = this.head
154+
let prev = this.head
155+
156+
while (currIdx < idx) {
157+
currIdx++
158+
prev = curr
159+
curr = curr.next
160+
}
161+
162+
prev.next = curr.next
163+
this.length--
164+
}
165+
}
166+
167+
search (val) {
168+
if (!this.head) return "Empty Linked List!"
169+
if (!this.head.next && this.head.val !== val) return null
170+
if (!this.head.next && this.head.val === val) return 0
171+
172+
let currIdx = 0
173+
let curr = this.head
174+
175+
while (curr) {
176+
if (curr.val === val) return currIdx
177+
currIdx++
178+
curr = curr.next
179+
}
180+
181+
return null
182+
}
183+
184+
toArray () {
185+
const arr = []
186+
if (!this.head) return null
187+
188+
let curr = this.head
189+
190+
while (curr) {
191+
arr.push (curr.val)
192+
curr = curr.next
193+
}
194+
195+
return arr
196+
}
197+
198+
reverse () {
199+
if (!this.head) throw new Error ("Empty Linked List")
200+
if (!this.head.next) return
201+
202+
let prev = null
203+
let curr = this.head
204+
let next = curr.next
205+
206+
while (curr) {
207+
next = curr.next
208+
curr.next = prev
209+
prev = curr
210+
curr = next
211+
}
212+
213+
this.head = prev
214+
}
215+
}
216+
217+
// SAMPLE USECASE
218+
219+
const list = new LinkedList()
220+
list.insertAtTail (10)
221+
list.insertAtTail (20)
222+
list.insertAtTail (30)
223+
list.insertAtTail (40)
224+
list.insertAtTail (50)
225+
list.reverse ()
226+
list.print ()

0 commit comments

Comments
 (0)