Skip to content

Commit 7b13775

Browse files
committed
LinkedList Complete Course Added
1 parent 4a8732f commit 7b13775

File tree

3 files changed

+320
-0
lines changed

3 files changed

+320
-0
lines changed
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
class Node {
2+
constructor(value) {
3+
this.head = value;
4+
this.next = null;
5+
}
6+
}
7+
8+
class LinkedList {
9+
constructor(value) {
10+
this.head = new Node(value);
11+
this.tail = this.head;
12+
this.length = 1;
13+
}
14+
15+
push(value) {
16+
let newNode = new Node(value);
17+
18+
if (!this.head) {
19+
this.head = newNode;
20+
this.tail = newNode;
21+
}
22+
23+
this.tail.next = newNode;
24+
this.tail = newNode;
25+
this.length++;
26+
}
27+
28+
pop() {
29+
if (!this.head) {
30+
return undefined;
31+
}
32+
33+
let temp = this.head;
34+
let prev = this.head;
35+
36+
while (temp.next) {
37+
// console.log("********** temp", temp);
38+
prev = temp;
39+
temp = prev.next;
40+
}
41+
42+
// console.log(prev);
43+
this.tail = prev;
44+
this.tail.next = null;
45+
this.length--;
46+
47+
if (this.length === 0) {
48+
this.head = null;
49+
this.tail = null;
50+
}
51+
52+
return temp;
53+
}
54+
55+
unshift(value) {
56+
const newNode = new Node(value);
57+
58+
if (!this.head) {
59+
this.head = newNode;
60+
this.tail = newNode;
61+
}
62+
63+
newNode.next = this.head;
64+
this.head = newNode;
65+
this.length++;
66+
return this;
67+
}
68+
69+
shift() {
70+
if (!this.head) {
71+
return undefined;
72+
}
73+
74+
// 1. Point to the first node/element
75+
let temp = this.head;
76+
// 2. Move the head to the next node/element
77+
this.head = this.head.next;
78+
// 3. Remove first element
79+
temp.next = null;
80+
this.length--;
81+
82+
// If we have one node in the list
83+
if (this.length === 0) {
84+
this.tail = null;
85+
}
86+
87+
return temp;
88+
}
89+
90+
getFirst() {
91+
return this.head;
92+
}
93+
94+
getLast() {
95+
if (!this.head) {
96+
return null;
97+
}
98+
99+
let node = this.head;
100+
101+
while (node) {
102+
// console.log("***********", node);
103+
if (!node.next) {
104+
return node;
105+
}
106+
node = node.next;
107+
}
108+
}
109+
110+
get(index) {
111+
let counter = 0;
112+
let node = this.head;
113+
114+
while (node) {
115+
if (counter === index) {
116+
return node;
117+
}
118+
119+
counter++;
120+
node = node.next;
121+
}
122+
123+
return null;
124+
}
125+
126+
set(index, value) {
127+
let temp = this.get(index);
128+
console.log("----------", temp);
129+
130+
if (temp) {
131+
temp.value = value;
132+
return true;
133+
}
134+
135+
return false;
136+
}
137+
138+
insert(index, value) {
139+
if (index === 0) {
140+
return this.unshift(value);
141+
}
142+
143+
if (index === this.length) {
144+
return this.push(value);
145+
}
146+
147+
const newNode = new Node(value);
148+
// Uses the get method to find the node right before the desired position (index - 1).
149+
const temp = this.get(index - 1);
150+
151+
newNode.next = temp.next;
152+
temp.next = newNode;
153+
this.length++;
154+
return true;
155+
}
156+
157+
size() {
158+
let counter = 0;
159+
let node = this.head;
160+
161+
while (node) {
162+
counter++;
163+
node = node.next;
164+
}
165+
166+
return counter;
167+
}
168+
169+
clear() {
170+
this.head = null;
171+
}
172+
}
173+
174+
const myLinkedList = new LinkedList(1);
175+
myLinkedList.push(2);
176+
myLinkedList.push(3);
177+
console.log(myLinkedList);
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
class Node {
2+
constructor(value) {
3+
this.value = value;
4+
this.next = null;
5+
this.prev = null;
6+
}
7+
}
8+
9+
class DoublyLinkedList {
10+
constructor(value) {
11+
const newNode = new Node(value);
12+
this.head = newNode;
13+
this.tail = this.head;
14+
this.length = 1;
15+
}
16+
17+
push(value) {
18+
const newNode = new Node(value);
19+
20+
if (!this.head) {
21+
this.head = newNode;
22+
this.tail = newNode;
23+
} else {
24+
this.tail.next = newNode;
25+
newNode.prev = this.tail;
26+
this.tail = newNode;
27+
}
28+
29+
this.length++;
30+
return this;
31+
}
32+
33+
pop() {
34+
if (this.length === 0) {
35+
return undefined;
36+
}
37+
38+
let temp = this.tail;
39+
40+
if (this.length === 1) {
41+
this.head = null;
42+
this.tail = null;
43+
} else {
44+
this.tail = this.tail.prev;
45+
this.tail.next = null;
46+
temp.prev = null;
47+
}
48+
49+
this.length--;
50+
return temp;
51+
}
52+
53+
unshift(value) {
54+
const newNode = new Node(value);
55+
56+
if (this.length === 0) {
57+
this.head = newNode;
58+
this.tail = newNode;
59+
} else {
60+
newNode.next = this.head;
61+
this.head.prev = newNode;
62+
this.head = newNode;
63+
}
64+
65+
this.length++;
66+
return this;
67+
}
68+
69+
shift() {
70+
if (this.length === 0) {
71+
return undefined;
72+
}
73+
74+
let temp = this.head;
75+
76+
if (this.length === 1) {
77+
this.head = null;
78+
this.tail = null;
79+
} else {
80+
this.head = this.head.next;
81+
this.head.prev = null;
82+
temp.next = null;
83+
}
84+
this.length--;
85+
return temp;
86+
}
87+
}
88+
89+
let myDoublyLinkedList = new DoublyLinkedList(0);
90+
myDoublyLinkedList.push(1);
91+
// myDoublyLinkedList.pop();
92+
// myDoublyLinkedList.shift();
93+
console.log(myDoublyLinkedList);
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
class Node {
2+
constructor(value) {
3+
this.head = value;
4+
this.next = null;
5+
}
6+
}
7+
8+
class LinkedList {
9+
constructor(value) {
10+
this.head = new Node(value);
11+
this.tail = this.head;
12+
this.length = 1;
13+
}
14+
15+
push(value) {
16+
let newNode = new Node(value);
17+
18+
if (!this.head) {
19+
this.head = newNode;
20+
this.tail = newNode;
21+
}
22+
23+
this.tail.next = newNode;
24+
this.tail = newNode;
25+
this.length++;
26+
}
27+
28+
reverse() {
29+
let temp = this.head;
30+
this.head = this.tail;
31+
this.tail = temp;
32+
let next = temp;
33+
let prev = null;
34+
35+
for (let i = 0; i < this.length; i++) {
36+
next = temp.next;
37+
temp.next = prev;
38+
prev = temp;
39+
temp = next;
40+
}
41+
42+
return this;
43+
}
44+
}
45+
46+
const myLinkedList = new LinkedList(1);
47+
myLinkedList.push(2);
48+
myLinkedList.push(3);
49+
myLinkedList.push(4);
50+
console.log(myLinkedList.reverse());

0 commit comments

Comments
 (0)