Untitled

mail@pastecode.io avatar
unknown
typescript
a year ago
530 B
11
Indexable
Never
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
    }
}