Untitled

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