Untitled
unknown
typescript
2 years ago
470 B
8
Indexable
type listNode<T> = { data: T; next: listNode<T> | null }
class LinkedList {
head: listNode<T>;
constructor() { this.head = { init, next: null }
append(data: T): listNode<T> {
const node: listNode<T> = { data, next: null };
if (this.head == undefined) { this.head = node }
let p = this.head;
while (this.hasNext(p)) {
p = p.next! // it won't work without the non-null assertion
}
p.next = node
return node
}
}
Editor is loading...