Last active
December 25, 2022 12:23
-
-
Save YoucefMegoura/78ae1bca3e0058f1ce4e2766ca7f4681 to your computer and use it in GitHub Desktop.
Linked List TypeScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LinkedListNode { | |
constructor(public value: any, public next: object){} | |
} | |
class LinkedList { | |
public head: LinkedListNode | any; | |
public tail: LinkedListNode | any; | |
public length: number; | |
constructor(value: any) { | |
this.head = new LinkedListNode(value, null); | |
this.tail = this.head; | |
this.length = 1; | |
} | |
generate(length: number): void { | |
for (let i = 0; i < length; i++) { | |
this.append(Math.round(Math.random() * 100)); | |
} | |
} | |
append(value: any): void { | |
this.length++; | |
let newNode: LinkedListNode = new LinkedListNode(value, null); | |
this.tail.next = newNode; | |
this.tail = newNode; | |
} | |
prepend(value: any): void { | |
this.length++; | |
let newNode: LinkedListNode = new LinkedListNode(value, this.head); | |
this.head = newNode; | |
} | |
lookup(index: number): LinkedListNode { | |
let pointer: LinkedListNode | any = this.head; | |
for (let i = 0; i < index - 1; i++) { | |
pointer = pointer.next; | |
} | |
return pointer; | |
} | |
getNode(index: number): any { | |
const pointer: LinkedListNode = this.lookup(index); | |
return pointer.value; | |
} | |
insert(value: any, index: number): void { | |
if (index >= this.length) this.append(value); | |
this.length++; | |
let newNode: LinkedListNode = new LinkedListNode(value, null); | |
let pointer: LinkedListNode = this.lookup(index); | |
newNode.next = pointer.next; | |
pointer.next = newNode; | |
} | |
removeFirst(): void { | |
this.length--; | |
this.head = this.head.next; | |
} | |
removeLast(): void { | |
this.length--; | |
let pointer: LinkedListNode = this.lookup(this.length); | |
pointer.next = null; | |
this.tail = pointer; | |
} | |
splice(index: number, length: number): void { | |
if (index + length > this.length) return; | |
this.length = this.length - length; | |
let pointer: LinkedListNode | any = this.lookup(index); | |
let pointer2: LinkedListNode | any = pointer; | |
for (let i = 0; i < length + 1; i++) { | |
pointer2 = pointer2.next; | |
} | |
pointer.next = pointer2; | |
} | |
remove(index: number): void { | |
this.splice(index, 1); | |
} | |
removeAll(): void { | |
this.length = 0; | |
delete this.head; | |
delete this.tail; | |
} | |
getAllNodesAsArray(): any { | |
let array: Array<any> = new Array<any>(); | |
let pointer: LinkedListNode | any = this.head; | |
while (pointer != null) { | |
array.push(pointer.value); | |
pointer = pointer.next; | |
} | |
return array; | |
} | |
reverse() : void { | |
if (this.length == 1) { | |
return this.head; | |
} | |
let leaderPointer: LinkedListNode | any = this.head.next; | |
let secondPointer: LinkedListNode | any = this.head; | |
this.tail = this.head; | |
while(leaderPointer) { | |
let temp = leaderPointer.next; | |
leaderPointer.next = secondPointer; | |
secondPointer = leaderPointer | |
leaderPointer = temp; | |
} | |
this.head.next = null; | |
this.head = secondPointer; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment