-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
101 additions
and
45 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { LinkedListNode } from "./linkedListNode"; | ||
|
||
export class LinkedList<T> { | ||
private head: LinkedListNode<T>; | ||
private tail: LinkedListNode<T>; | ||
|
||
constructor() { | ||
this.head = new LinkedListNode(null as unknown as T); | ||
this.tail = this.head; | ||
} | ||
|
||
insertFirst(value: T): LinkedListNode<T> { | ||
const node = new LinkedListNode(value); | ||
if (this.isEmpty()) { | ||
this.tail = node; | ||
this.head.setNextNode(node); | ||
} else { | ||
node.setNextNode(this.head.getNext()); | ||
this.head.setNextNode(node); | ||
} | ||
|
||
return node; | ||
} | ||
|
||
insertLast(value: T): LinkedListNode<T> { | ||
if (this.isEmpty()) { | ||
return this.insertFirst(value); | ||
} | ||
|
||
const node = new LinkedListNode(value); | ||
this.tail?.setNextNode(node); | ||
this.tail = node; | ||
return node; | ||
} | ||
|
||
isEmpty() { | ||
return !this.head.hasNext(); | ||
} | ||
|
||
getHead() { | ||
return this.head; | ||
} | ||
|
||
getTail() { | ||
return this.tail; | ||
} | ||
|
||
toArray() { | ||
const arr = []; | ||
let cur = this.head; | ||
while (cur.hasNext()) { | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
cur = cur.getNext()!; | ||
arr.push(cur); | ||
} | ||
|
||
return arr; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
export class LinkedListNode<T> { | ||
private value: T; | ||
private next: LinkedListNode<T> | null; | ||
|
||
constructor(value: T) { | ||
this.value = value; | ||
this.next = null; | ||
} | ||
|
||
setNextNode(node: LinkedListNode<T> | null) { | ||
this.next = node; | ||
|
||
return node; | ||
} | ||
|
||
getValue() { | ||
return this.value; | ||
} | ||
|
||
getNext() { | ||
return this.next; | ||
} | ||
|
||
hasNext() { | ||
return this.next instanceof LinkedListNode; | ||
} | ||
} |