Skip to content

Commit 841135f

Browse files
committed
reverse linked list
1 parent 4208bdb commit 841135f

File tree

2 files changed

+170
-135
lines changed

2 files changed

+170
-135
lines changed

Data Structures/Linked Lists/index.js

Lines changed: 136 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -11,146 +11,147 @@
1111
// console.log(n1);
1212

1313
class Node {
14-
constructor(data, next = null) {
15-
this.data = data;
16-
this.next = next;
17-
}
14+
constructor(data, next = null) {
15+
this.data = data;
16+
this.next = next;
17+
}
1818
}
1919

2020
const n1 = new Node(100);
2121

22-
// console.log(n1);
23-
2422
class LinkedList {
25-
constructor() {
26-
this.head = null;
27-
this.size = 0;
28-
}
29-
30-
// Search first node with key k
31-
listSearch(data) {
32-
if (!this.head) return null;
33-
let current = this.head;
34-
while (current !== null && current.data !== data) {
35-
current = current.next;
36-
}
37-
38-
return current;
39-
}
40-
41-
// Insert first node
42-
insertFirst(data) {
43-
this.head = new Node(data, this.head);
44-
this.size++;
45-
}
46-
47-
// Insert last node
48-
insertLast(data) {
49-
let node = new Node(data);
50-
let current;
51-
52-
// If empty, make this node as the head
53-
if (!this.head) {
54-
this.head = node;
55-
} else {
56-
current = this.head;
57-
while (current.next) {
58-
current = current.next;
59-
}
60-
61-
current.next = node;
62-
}
63-
64-
this.size++;
65-
}
66-
67-
// Insert at index
68-
insertIndex(data, index) {
69-
// If index is out of range
70-
if (index > 0 && index > this.size) return;
71-
72-
// If index = 0
73-
if (index === 0) {
74-
this.insertFirst(data);
75-
return;
76-
}
77-
78-
const node = new Node(data);
79-
let current, previous;
80-
81-
// Set current to first
82-
current = this.head;
83-
let count = 0;
84-
85-
while (count < index) {
86-
previous = current; // Node before the index
87-
count++;
88-
current = current.next; // Node after the index
89-
}
90-
91-
node.next = current;
92-
previous.next = node;
93-
94-
this.size++;
95-
}
96-
97-
// Get at index
98-
getAt(index) {
99-
let current = this.head;
100-
let count = 0;
101-
102-
while (current) {
103-
if (count === index) {
104-
console.log(current.data);
105-
}
106-
count++;
107-
current = current.next;
108-
}
109-
110-
return null;
111-
}
112-
113-
// Remove at index
114-
removeAt(index) {
115-
if (index > 0 && index > this.size) return;
116-
117-
let current = this.head;
118-
let previous;
119-
let count = 0;
120-
121-
// Remove first node
122-
if (index === 0) {
123-
this.head = current.next;
124-
} else {
125-
while (count < index) {
126-
count++;
127-
previous = current;
128-
current = current.next;
129-
}
130-
131-
previous.next = current.next;
132-
}
133-
134-
this.size--;
135-
}
136-
137-
// Clear the list
138-
clearList() {
139-
this.head = null;
140-
this.size = 0;
141-
}
142-
143-
// Print the list data
144-
printListData() {
145-
let current = this.head;
146-
147-
while (current) {
148-
console.log(current.data);
149-
current = current.next;
150-
}
151-
}
23+
constructor() {
24+
this.head = null;
25+
this.size = 0;
26+
}
27+
28+
// Search first node with key k
29+
listSearch(data) {
30+
if (!this.head) return null;
31+
let current = this.head;
32+
while (current !== null && current.data !== data) {
33+
current = current.next;
34+
}
35+
36+
return current;
37+
}
38+
39+
// Insert first node
40+
insertFirst(data) {
41+
this.head = new Node(data, this.head);
42+
this.size++;
43+
}
44+
45+
// Insert last node
46+
insertLast(data) {
47+
let node = new Node(data);
48+
let current;
49+
50+
// If empty, make this node as the head
51+
if (!this.head) {
52+
this.head = node;
53+
} else {
54+
current = this.head;
55+
while (current.next) {
56+
current = current.next;
57+
}
58+
59+
current.next = node;
60+
}
61+
62+
this.size++;
63+
}
64+
65+
// Insert at index
66+
insertIndex(data, index) {
67+
// If index is out of range
68+
if (index > 0 && index > this.size) return;
69+
70+
// If index = 0
71+
if (index === 0) {
72+
this.insertFirst(data);
73+
return;
74+
}
75+
76+
const node = new Node(data);
77+
let current, previous;
78+
79+
// Set current to first
80+
current = this.head;
81+
let count = 0;
82+
83+
while (count < index) {
84+
previous = current; // Node before the index
85+
count++;
86+
current = current.next; // Node after the index
87+
}
88+
89+
node.next = current;
90+
previous.next = node;
91+
92+
this.size++;
93+
}
94+
95+
// Get at index
96+
getAt(index) {
97+
let current = this.head;
98+
let count = 0;
99+
100+
while (current) {
101+
if (count === index) {
102+
console.log(current.data);
103+
return current.data;
104+
}
105+
count++;
106+
current = current.next;
107+
}
108+
109+
return null;
110+
}
111+
112+
// Remove at index
113+
removeAt(index) {
114+
if (index > 0 && index > this.size) return;
115+
116+
let current = this.head;
117+
let previous;
118+
let count = 0;
119+
120+
// Remove first node
121+
if (index === 0) {
122+
this.head = current.next;
123+
} else {
124+
while (count < index) {
125+
count++;
126+
previous = current;
127+
current = current.next;
128+
}
129+
130+
previous.next = current.next;
131+
}
132+
133+
this.size--;
134+
}
135+
136+
// Clear the list
137+
clearList() {
138+
this.head = null;
139+
this.size = 0;
140+
}
141+
142+
// Print the list data
143+
printListData() {
144+
let current = this.head;
145+
146+
while (current) {
147+
// console.log(current.data);
148+
current = current.next;
149+
}
150+
}
152151
}
153152

153+
module.exports = LinkedList;
154+
154155
const ll = new LinkedList();
155156
ll.insertFirst(300);
156157
ll.insertFirst(100);
@@ -166,5 +167,5 @@ ll.removeAt(11);
166167
// ll.clearList();
167168
ll.printListData();
168169
ll.getAt(10);
169-
console.log(JSON.stringify(ll, 0, 2));
170-
console.log(JSON.stringify(ll.listSearch(500), 0, 2));
170+
// console.log(JSON.stringify(ll, 0, 2));
171+
// console.log(JSON.stringify(ll.listSearch(500), 0, 2));
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
*
3+
* Given a linkedlist including N elements a_1 => a_2 => ... => a_n
4+
* Write a function reverse this linkedlist: a_n => a_n-1 => ... => a_1
5+
*
6+
*/
7+
const LinkedList = require("./index");
8+
9+
const ll = new LinkedList();
10+
ll.insertLast(1);
11+
ll.insertLast(2);
12+
ll.insertLast(3);
13+
14+
const reverseLinkedlist = linkedList => {
15+
console.log(
16+
"The input linkedlist is: ",
17+
JSON.stringify(linkedList, null, 2)
18+
);
19+
let arr = [];
20+
while (linkedList.size > 1) {
21+
let curr = linkedList.getAt(0);
22+
linkedList.removeAt(0);
23+
arr.push(curr);
24+
}
25+
for (let i = arr.length - 1; i >= 0; i--) {
26+
linkedList.insertLast(arr[i]);
27+
}
28+
console.log(
29+
"The output linkedlist is: ",
30+
JSON.stringify(linkedList, null, 2)
31+
);
32+
return 0;
33+
};
34+
reverseLinkedlist(ll);

0 commit comments

Comments
 (0)