Untitled

 avatar
unknown
typescript
2 years ago
530 B
15
Indexable
type listNode<T> = { data: T; next: listNode<T> | null }
class LinkedList {
  head: listNode<T>;
  constructor() { this.head = { init, next: null }
  hasNext(p: listNode<T>): boolean { return p.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...