Skip to content

Commit 24f97e1

Browse files
committed
Adding source code and practice questions of linked list
1 parent d736814 commit 24f97e1

File tree

3 files changed

+333
-2
lines changed

3 files changed

+333
-2
lines changed

Linked List/README.md

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
# Linked List in JavaScript
2+
3+
<p align="center">
4+
<a href="https://youtu.be/ATKPTiZgT3Q">
5+
<img src="https://img.youtube.com/vi/ATKPTiZgT3Q/0.jpg" alt="Linked List in JavaScript" />
6+
</a>
7+
</p>
8+
9+
## Source Code
10+
```javascript
11+
class Node {
12+
constructor(data) {
13+
this.data = data;
14+
this.next = null;
15+
}
16+
}
17+
18+
class LinkedList {
19+
constructor() {
20+
this.head = null;
21+
this.size = 0;
22+
}
23+
24+
insertAtHead(data) {
25+
const newNode = new Node(data);
26+
newNode.next = this.head;
27+
this.head = newNode;
28+
this.size++;
29+
}
30+
31+
insertAt(index, data) {
32+
if (index < 0 || index > this.size) {
33+
return "Invalid Index"
34+
}
35+
36+
if (index === 0) {
37+
this.insertAtHead(data)
38+
return;
39+
}
40+
41+
let newNode = new Node(data)
42+
let temp = this.head;
43+
for (let i = 0; i < index - 1; i++) {
44+
temp = temp.next;
45+
}
46+
47+
newNode.next = temp.next;
48+
temp.next = newNode;
49+
50+
this.size++;
51+
}
52+
53+
print() {
54+
let result = ""
55+
let temp = this.head;
56+
while (temp) {
57+
result += `${temp.data}->`
58+
temp = temp.next;
59+
}
60+
return result
61+
}
62+
63+
removeAtHead() {
64+
if (this.isEmpty()) {
65+
return "List is already empty"
66+
}
67+
68+
this.head = this.head.next;
69+
this.size--;
70+
}
71+
72+
removeElement(data) {
73+
if (this.isEmpty()) {
74+
return "List is already empty"
75+
}
76+
77+
let current = this.head, prev = null;
78+
while (current) {
79+
if (current.data === data) {
80+
if (prev === null) {
81+
this.head = current.next;
82+
} else {
83+
prev.next = current.next;
84+
}
85+
this.size--;
86+
return current.element
87+
}
88+
prev = current;
89+
current = prev.next;
90+
}
91+
return -1;
92+
}
93+
94+
searchElement(data) {
95+
let curr = this.head;
96+
let index = 0;
97+
98+
while (curr) {
99+
if (curr.data === data) {
100+
return index;
101+
}
102+
index++;
103+
curr = curr.next;
104+
}
105+
return -1;
106+
}
107+
108+
middleNode() {
109+
let slow = this.head, fast = this.head;
110+
while (fast && fast.next) {
111+
fast = fast.next.next;
112+
slow = slow.next;
113+
}
114+
return slow;
115+
};
116+
117+
reverse() {
118+
let prev = null, curr = this.head, next;
119+
while (curr) {
120+
next = curr.next;
121+
curr.next = prev;
122+
prev = curr;
123+
curr = next;
124+
}
125+
this.head = prev;
126+
}
127+
128+
isCycle() {
129+
let slow = this.head, fast = this.head;
130+
while (fast && fast.next) {
131+
fast = fast.next.next;
132+
slow = slow.next;
133+
134+
if (slow === fast) {
135+
return true;
136+
}
137+
}
138+
return false;
139+
}
140+
141+
isEmpty() {
142+
return this.size === 0;
143+
}
144+
}
145+
146+
let list = new LinkedList()
147+
list.insertAtHead(43) // 43
148+
list.insertAtHead(50) // 50->43
149+
list.insertAtHead(34) // 34->50->43
150+
list.insertAt(2, 46) // 34->50->46->43
151+
list.removeAtHead() // 50->46->43
152+
list.removeElement(46) // 50->43
153+
list.reverse() // 43->50
154+
console.log(list.isCycle()) // false
155+
console.log(list.middleNode()) // 50
156+
console.log(list.searchElement(50)) //1
157+
console.log(list.print()) // 43->50
158+
```
159+
160+
## Practice Questions:
161+
162+
**Note**: You can find solutions of all these problems in [this playlist](https://www.youtube.com/playlist?list=PLSH9gf0XETourRyZW56Rdh9e0Phx-AJM5)
163+
164+
Before checking the solutions, challenge yourself to solve the problems on your own. If you're stuck, watch the solution video up to the intuition part. Then, code it yourself before watching the complete solution.
165+
166+
This approach builds solid problem-solving skills.
167+
168+
169+
- [Middle of Linked List](https://leetcode.com/problems/middle-of-the-linked-list/)
170+
- [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/)
171+
- [Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/)
172+
- [Remove Duplicates from Sorted list I](https://leetcode.com/problems/remove-duplicates-from-sorted-list/)
173+
- [Remove Duplicates from Sorted list II](https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/)
174+
- [Linked List Cycle I](https://leetcode.com/problems/linked-list-cycle/)
175+
- [Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/)
176+
- [Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists/)
177+
- [Next Greater Node in Linked List](https://leetcode.com/problems/next-greater-node-in-linked-list/)
178+
- [Remove Zero Consecutive Nodes from Linked List](https://leetcode.com/problems/remove-zero-sum-consecutive-nodes-from-linked-list/)
179+
- [Reverse Linked List II](https://leetcode.com/problems/reverse-linked-list-ii/)
180+
- [Odd Even Linked list](https://leetcode.com/problems/odd-even-linked-list/)
181+
- [Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/)
182+
- [Reorder List](https://leetcode.com/problems/reorder-list/)
183+
- [Remove Nth Node from End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/)
184+
- [Merge K Sorted Lists](https://leetcode.com/problems/merge-k-sorted-lists/)

Linked List/index.js

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
class Node {
2+
constructor(data) {
3+
this.data = data;
4+
this.next = null;
5+
}
6+
}
7+
8+
class LinkedList {
9+
constructor() {
10+
this.head = null;
11+
this.size = 0;
12+
}
13+
14+
insertAtHead(data) {
15+
const newNode = new Node(data);
16+
newNode.next = this.head;
17+
this.head = newNode;
18+
this.size++;
19+
}
20+
21+
insertAt(index, data) {
22+
if (index < 0 || index > this.size) {
23+
return "Invalid Index"
24+
}
25+
26+
if (index === 0) {
27+
this.insertAtHead(data)
28+
return;
29+
}
30+
31+
let newNode = new Node(data)
32+
let temp = this.head;
33+
for (let i = 0; i < index - 1; i++) {
34+
temp = temp.next;
35+
}
36+
37+
newNode.next = temp.next;
38+
temp.next = newNode;
39+
40+
this.size++;
41+
}
42+
43+
print() {
44+
let result = ""
45+
let temp = this.head;
46+
while (temp) {
47+
result += `${temp.data}->`
48+
temp = temp.next;
49+
}
50+
return result
51+
}
52+
53+
removeAtHead() {
54+
if (this.isEmpty()) {
55+
return "List is already empty"
56+
}
57+
58+
this.head = this.head.next;
59+
this.size--;
60+
}
61+
62+
removeElement(data) {
63+
if (this.isEmpty()) {
64+
return "List is already empty"
65+
}
66+
67+
let current = this.head, prev = null;
68+
while (current) {
69+
if (current.data === data) {
70+
if (prev === null) {
71+
this.head = current.next;
72+
} else {
73+
prev.next = current.next;
74+
}
75+
this.size--;
76+
return current.element
77+
}
78+
prev = current;
79+
current = prev.next;
80+
}
81+
return -1;
82+
}
83+
84+
searchElement(data) {
85+
let curr = this.head;
86+
let index = 0;
87+
88+
while (curr) {
89+
if (curr.data === data) {
90+
return index;
91+
}
92+
index++;
93+
curr = curr.next;
94+
}
95+
return -1;
96+
}
97+
98+
middleNode() {
99+
let slow = this.head, fast = this.head;
100+
while (fast && fast.next) {
101+
fast = fast.next.next;
102+
slow = slow.next;
103+
}
104+
return slow;
105+
};
106+
107+
reverse() {
108+
let prev = null, curr = this.head, next;
109+
while (curr) {
110+
next = curr.next;
111+
curr.next = prev;
112+
prev = curr;
113+
curr = next;
114+
}
115+
this.head = prev;
116+
}
117+
118+
isCycle() {
119+
let slow = this.head, fast = this.head;
120+
while (fast && fast.next) {
121+
fast = fast.next.next;
122+
slow = slow.next;
123+
124+
if (slow === fast) {
125+
return true;
126+
}
127+
}
128+
return false;
129+
}
130+
131+
isEmpty() {
132+
return this.size === 0;
133+
}
134+
}
135+
136+
let list = new LinkedList()
137+
list.insertAtHead(43) // 43
138+
list.insertAtHead(50) // 50->43
139+
list.insertAtHead(34) // 34->50->43
140+
list.insertAt(2, 46) // 34->50->46->43
141+
list.removeAtHead() // 50->46->43
142+
list.removeElement(46) // 50->43
143+
list.reverse() // 43->50
144+
console.log(list.isCycle()) // false
145+
console.log(list.middleNode()) // 50
146+
console.log(list.searchElement(50)) //1
147+
console.log(list.print()) // 43->50

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@
3131
- [Linear & Binary Search](https://github.com/Vishal-raj-1/DSA-In-JS-With-Vishal/blob/main/Searching%20Algorthims/README.md)
3232
- [Objects](https://github.com/Vishal-raj-1/DSA-In-JS-With-Vishal/blob/main/Objects/README.md)
3333
- [Sorting](https://github.com/Vishal-raj-1/DSA-In-JS-With-Vishal/blob/main/Sorting/README.md)
34-
- [Set & Map](https://github.com/Vishal-raj-1/DSA-In-JS-With-Vishal/tree/main/Set%20%26%20Map)
34+
- [Set & Map](https://github.com/Vishal-raj-1/DSA-In-JS-With-Vishal/blob/main/Set%20%26%20Map/README.md)
35+
- [Linked List](https://github.com/Vishal-raj-1/DSA-In-JS-With-Vishal/tree/main/Linked%20%List/README.md)
3536

3637
## Upcoming Topics
3738

38-
- Linked List
3939
- Stack
4040
- Queue
4141
- Binary Tree

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy