All files / leetCode List.ts

100% Statements 17/17
100% Branches 6/6
100% Functions 3/3
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41              288x 288x         107x 29x   78x   78x 171x 171x     78x       1x   1x 5x   5x 4x     1x        
import consola from 'consola'
 
class ListNode<T> {
  val: T
  next: ListNode<T> | null
 
  constructor(val: T, next?: ListNode<T> | null) {
    this.val = val
    this.next = next === undefined ? null : next
  }
}
 
function arrayToList<T>(nodes: T[]): ListNode<T> | null {
  if (nodes.length === 0)
    return null
 
  let list = new ListNode(nodes[nodes.length - 1])
 
  for (let i = nodes.length - 2; i >= 0; i--) {
    const current = new ListNode(nodes[i], list)
    list = current
  }
 
  return list
}
 
function printList<T>(list: ListNode<T> | null): void {
  let output = ''
 
  for (let node = list; node !== null; node = node.next) {
    output += `[${String(node.val)}]`
 
    if (node.next !== null)
      output += ' -> '
  }
 
  consola.info(output)
}
 
export { arrayToList, ListNode, printList }