Step 1 of 4

Node Pointers & Boundary Insertion

Start by implementing append(val) and prepend(val).

  • When the list is empty (this.head === null), both head and tail point to the newly created ListNode.
  • append(val): Attach the new node to this.tail.next and update this.tail in $O(1)$ time.
  • prepend(val): Point node.next to this.head and update this.head in $O(1)$ time.
  • Increment this.length.

Your build

Running is free — Submit is what records the step. Or press ⌘↩

Tests

1 case
calltypeexpectedresult
runOps([["append",10],["append",20],["prepend",5],["toArray"],["size"]])appends and prepends nodes[null,null,null,[5,10,20],3]

Hints

Stuck? Hints open one at a time, each giving a little more away.

2 hints left