Skip to content

Commit 4cc4578

Browse files
committed
Linked List
1 parent c752849 commit 4cc4578

File tree

3 files changed

+182
-0
lines changed

3 files changed

+182
-0
lines changed

#9 - Linked List/1-linked-list.js

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
// Linked List in Javascript
2+
3+
class Node {
4+
constructor(data) {
5+
this.data = data;
6+
this.next = null;
7+
}
8+
}
9+
10+
class LinkedList {
11+
constructor() {
12+
this.head = null;
13+
}
14+
15+
addFirst(data) {
16+
const newNode = new Node(data);
17+
newNode.next = this.head;
18+
this.head = newNode;
19+
}
20+
21+
addLast(data) {
22+
const newNode = new Node(data);
23+
24+
if (!this.head) {
25+
this.head = newNode;
26+
return;
27+
}
28+
29+
let current = this.head;
30+
while (current.next) {
31+
current = current.next;
32+
}
33+
34+
current.next = newNode;
35+
}
36+
37+
size() {
38+
let count = 0;
39+
let current = this.head;
40+
while (current) {
41+
count++;
42+
current = current.next;
43+
}
44+
return count;
45+
}
46+
47+
addAt(index, data) {
48+
if (index < 0 || index > this.size()) {
49+
console.error("Invalid Index");
50+
return;
51+
}
52+
53+
const newNode = new Node(data);
54+
if (index === 0) {
55+
newNode.next = this.head;
56+
this.head = newNode;
57+
return;
58+
}
59+
60+
let current = this.head;
61+
for (let i = 0; i < index - 1; i++) {
62+
current = current.next;
63+
}
64+
65+
newNode.next = current.next;
66+
current.next = newNode;
67+
}
68+
69+
removeTop() {
70+
if (!this.head) {
71+
return;
72+
}
73+
74+
this.head = this.head.next;
75+
}
76+
77+
removeLast() {
78+
if (!this.head) {
79+
return;
80+
}
81+
82+
let current = this.head;
83+
while (current.next.next) {
84+
current = current.next;
85+
}
86+
87+
current.next = null;
88+
}
89+
90+
removeAt(index) {
91+
if (index < 0 || index > this.size()) {
92+
console.error("Invalid Index");
93+
return;
94+
}
95+
96+
if (index === 0) {
97+
this.head = this.head.next;
98+
return;
99+
}
100+
101+
let current = this.head;
102+
for (let i = 0; i < index - 1; i++) {
103+
current = current.next;
104+
}
105+
106+
if (current.next) {
107+
current.next = current.next.next;
108+
}
109+
}
110+
111+
print() {
112+
let current = this.head;
113+
while (current) {
114+
console.log(current.data);
115+
current = current.next;
116+
}
117+
}
118+
}
119+
120+
const linkedlist = new LinkedList();
121+
122+
linkedlist.addFirst(5);
123+
linkedlist.addFirst(3);
124+
linkedlist.addFirst(8);
125+
linkedlist.addLast(6);
126+
127+
linkedlist.removeTop();
128+
129+
linkedlist.addAt(2, 8);
130+
131+
linkedlist.removeLast();
132+
linkedlist.removeAt(2);
133+
134+
linkedlist.print();
135+
console.log("size = " + linkedlist.size());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Ques 1 : Given the head of a singly linked list, return true if it is
2+
// a palindrome or false otherwise
3+
4+
// Input: head = [1,2,2,1] ----->>>>> Output: true;
5+
// Input: head = [1,2] ----->>>>> Output: false;
6+
7+
/**
8+
* Definition for singly-linked list.
9+
* function ListNode(val, next) {
10+
* this.val = (val===undefined ? 0 : val)
11+
* this.next = (next===undefined ? null : next)
12+
* }
13+
*/
14+
var isPalindrome = function (head) {
15+
let string1 = (string2 = "");
16+
let node = head;
17+
18+
while (node != null) {
19+
string1 = `${string1}${node.val}`;
20+
string2 = `${node.val}${string2}`;
21+
node = node.next;
22+
}
23+
return string1 === string2;
24+
};
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Ques 2 : Given the head of a singly linked list, reverse the list, and
2+
// return the reversed list.
3+
4+
// Input: head = [1,2] ----->>>>> Output: [2,1];
5+
6+
/**
7+
* Definition for singly-linked list.
8+
* function ListNode(val, next) {
9+
* this.val = (val===undefined ? 0 : val)
10+
* this.next = (next===undefined ? null : next)
11+
* }
12+
*/
13+
var reverseList = (head) => {
14+
let prev = null;
15+
let current = head;
16+
while (current !== null) {
17+
const nextNode = current.next;
18+
current.next = prev;
19+
prev = current;
20+
current = nextNode;
21+
}
22+
return prev;
23+
};

0 commit comments

Comments
 (0)