Skip to content

Commit 5249e9a

Browse files
Added reverse method to Singly LL (Closes #5)
1 parent e15fbdb commit 5249e9a

File tree

5 files changed

+80
-29
lines changed

5 files changed

+80
-29
lines changed

Data-Structures/Linked-Lists/DoublyLinkedList.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,9 @@ if (import.meta.main) {
273273
ATLA.removeElementAtIndex(0, true);
274274
ATLA.removeElementAtIndex(1, true);
275275
ATLA.removeElementAtIndex(0, true);
276-
console.log('----------------------------------');
277-
278276
printLinkedList(ATLA);
277+
278+
console.log('----------------------------------');
279279
console.log();
280280

281281
ATLA.insert('Katara', 0, true);
@@ -359,8 +359,8 @@ if (import.meta.main) {
359359
// Removing element at index 1: "Sokka"
360360
// Removing element at index 0: "Zuko"
361361
// Removing element at index 0: "Iroh"
362-
// ----------------------------------
363362
// Empty linked list -_-
363+
// ----------------------------------
364364
//
365365
// --------- Prepending Katara ---------
366366
// --- Node Count: 1

Data-Structures/Linked-Lists/LinkedList.ts

Lines changed: 70 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,31 @@ export default class LinkedList<T> {
141141
return currentNode;
142142
}
143143

144+
public reverse(): boolean {
145+
if (this.length < 2 || !this.head || !this.tail) return false;
146+
147+
this.tail = this.head; // Current head will become the new tail
148+
149+
let first = this.head;
150+
let second = first.getNext();
151+
let tempNode; // Will be the third sequential node in deletion sequence
152+
153+
// Travel up the tree while the second node is present, i.e. not null
154+
while(second) {
155+
tempNode = second.getNext();
156+
157+
second.setNext(first);
158+
159+
first = second;
160+
second = tempNode;
161+
}
162+
163+
this.tail.setNext(null);
164+
this.head = first;
165+
166+
return true;
167+
}
168+
144169
public toString(nodesPerGroup?: number): string {
145170
if (this.length === 0 || !this.head) {
146171
return "";
@@ -214,6 +239,11 @@ if (import.meta.main) {
214239
printSearchFor('Zuko', ATLA);
215240
printGetValueAtIndex(2, ATLA);
216241
printGetValueAtIndex(5, ATLA);
242+
243+
console.log('------------ Reversing List ------------');
244+
ATLA.reverse();
245+
printLinkedList(ATLA);
246+
217247
console.log('----------------------------------');
218248

219249
printRemoveIndex(1, ATLA);
@@ -234,6 +264,10 @@ if (import.meta.main) {
234264
ATLA.append('Aang');
235265
printLinkedList(ATLA);
236266

267+
console.log('------------ Reversing List ------------');
268+
ATLA.reverse();
269+
printLinkedList(ATLA);
270+
237271
// RUN: deno run Data-Structures/Linked-Lists/LinkedList.ts
238272
}
239273

@@ -243,7 +277,7 @@ if (import.meta.main) {
243277
// HEAD { value: "Appa", next: true }
244278
// 1 { value: "Sokka", next: true }
245279
// 2 { value: "Katara", next: false } TAIL
246-
280+
//
247281
// --- Node Count: 6
248282
// HEAD { value: "Appa", next: true }
249283
// 1 { value: "Zuko", next: true }
@@ -259,40 +293,54 @@ if (import.meta.main) {
259293
// "Zuko" found at index: 1
260294
// Element at index 2: Sokka
261295
// Element at index 5: Katara
296+
// ------------ Reversing List ------------
297+
// --- Node Count: 6
298+
// HEAD { value: "Katara", next: true }
299+
// 1 { value: "Iroh", next: true }
300+
// 2 { value: "Aang", next: true }
301+
// 3 { value: "Sokka", next: true }
302+
// 4 { value: "Zuko", next: true }
303+
// 5 { value: "Appa", next: false } TAIL
304+
//
262305
// ----------------------------------
263-
// Removing element at index 1: "Zuko"
306+
// Removing element at index 1: "Iroh"
264307
// --- Node Count: 5
265-
// HEAD { value: "Appa", next: true }
266-
// 1 { value: "Sokka", next: true }
267-
// 2 { value: "Aang", next: true }
268-
// 3 { value: "Iroh", next: true }
269-
// 4 { value: "Katara", next: false } TAIL
308+
// HEAD { value: "Katara", next: true }
309+
// 1 { value: "Aang", next: true }
310+
// 2 { value: "Sokka", next: true }
311+
// 3 { value: "Zuko", next: true }
312+
// 4 { value: "Appa", next: false } TAIL
270313
//
271-
// Removing element at index 0: "Appa"
314+
// Removing element at index 0: "Katara"
272315
// --- Node Count: 4
273-
// HEAD { value: "Sokka", next: true }
274-
// 1 { value: "Aang", next: true }
275-
// 2 { value: "Iroh", next: true }
276-
// 3 { value: "Katara", next: false } TAIL
316+
// HEAD { value: "Aang", next: true }
317+
// 1 { value: "Sokka", next: true }
318+
// 2 { value: "Zuko", next: true }
319+
// 3 { value: "Appa", next: false } TAIL
277320
//
278-
// Removing element at index 2: "Iroh"
321+
// Removing element at index 2: "Zuko"
279322
// --- Node Count: 3
280-
// HEAD { value: "Sokka", next: true }
281-
// 1 { value: "Aang", next: true }
282-
// 2 { value: "Katara", next: false } TAIL
323+
// HEAD { value: "Aang", next: true }
324+
// 1 { value: "Sokka", next: true }
325+
// 2 { value: "Appa", next: false } TAIL
283326
//
284-
// Removing element at index 2: "Katara"
327+
// Removing element at index 2: "Appa"
285328
// --- Node Count: 2
286-
// HEAD { value: "Sokka", next: true }
287-
// 1 { value: "Aang", next: false } TAIL
329+
// HEAD { value: "Aang", next: true }
330+
// 1 { value: "Sokka", next: false } TAIL
288331
//
289-
// Removing element at index 1: "Aang"
290-
// Removing element at index 0: "Sokka"
332+
// Removing element at index 1: "Sokka"
333+
// Removing element at index 0: "Aang"
291334
// Empty linked list -_-
292335
// ----------------------------------
293336
// --- Node Count: 1
294337
// HEAD { value: "Katara", next: false } TAIL
295338
//
296339
// --- Node Count: 2
297340
// HEAD { value: "Katara", next: true }
298-
// 1 { value: "Aang", next: false } TAIL
341+
// 1 { value: "Aang", next: false } TAIL
342+
//
343+
// ------------ Reversing List ------------
344+
// --- Node Count: 2
345+
// HEAD { value: "Aang", next: true }
346+
// 1 { value: "Katara", next: false } TAIL

Data-Structures/Linked-Lists/SinglyNode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default class LinkedListNode<T> {
1111
this.value = value;
1212
}
1313

14-
public setNext(nextNode: LinkedListNode<T>) {
14+
public setNext(nextNode: LinkedListNode<T> | null) {
1515
this.next = nextNode;
1616
}
1717

Data-Structures/README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
- [X] Hash Tables
66
- [X] Stacks
77
- [X] Queues
8-
- [ ] Linked Lists
8+
- [X] Linked Lists
99
- [X] Trees
1010
- [X] Binary Search Tree (BST)
1111
- AVL Tree
@@ -20,8 +20,11 @@
2020
- [Comprehensive List of Data Structures](https://en.wikipedia.org/wiki/List_of_data_structures "Wikipedia: DS List")
2121
- [Visualizing Data Structures & Algorithms](https://visualgo.net/en)
2222

23-
### Trees
23+
### Foundations
24+
- [Arrays vs Linked Lists: Youtube](https://youtu.be/DyG9S9nAlUM)
2425
- [Linked List: VisuAlgo](https://visualgo.net/en/list)
26+
27+
### Trees
2528
- [Binary Search Tree: VisuAlgo](https://visualgo.net/bn/bst?slide=1)
2629
- [Binary Heap: VisuAlgo](https://visualgo.net/en/heap)
2730
- [AVL Tree Visualization](https://www.cs.usfca.edu/~galles/visualization/AVLtree.html)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
- [X] Hash Tables
1313
- [X] Stacks
1414
- [X] Queues
15-
- [ ] Linked Lists
15+
- [X] Linked Lists
1616
- [X] Trees
1717
- [ ] Graphs
1818

0 commit comments

Comments
 (0)