Step 3 of 4

Node Deletion

You start from the build so far — your own work where you have written it, the reference build where you have not. Either way this step stands on its own.

Implement deleteAt(index) to unlink and remove nodes.

  • If index < 0 or index >= length, return null / None.
  • If deleting head (index === 0), update this.head = this.head.next. If the list only had 1 node, also clear this.tail = null.
  • If deleting an internal or tail node, traverse to index - 1 and rewire prev.next = prev.next.next. If deleting the last node, update this.tail = prev.
  • Decrement this.length and return the removed node's value.

Your build

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

Tests

1 case
calltypeexpectedresult
runOps([["append","A"],["append","B"],["append","C"],["deleteAt",1],["toArray"],["deleteAt",0],["toArray"],["deleteAt",0],["toArray"],["size"]])deletes nodes from head, middle, and tail[null,null,null,"B",["A","C"],"A",["C"],"C",[],0]

Hints

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

2 hints left