Step 2 of 4

Index Access & Insertion

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 sequential index traversal with getAt(index) and insertAt(index, val).

  • getAt(index): Walk index steps from head and return the node's value. If index < 0 or index >= length, return null / None.
  • insertAt(index, val):
    • If index === 0, delegate to prepend(val).
    • If index === length, delegate to append(val).
    • Otherwise, traverse to index - 1 and rewire prev.next to insert the new node.
    • Return true on success, or false if index is out of bounds.

Your build

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

Tests

1 case
calltypeexpectedresult
runOps([["append",1],["append",3],["insertAt",1,2],["toArray"],["getAt",1],["getAt",99],["insertAt",-1,0]])gets and inserts at arbitrary indices[null,null,true,[1,2,3],2,null,false]

Hints

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

2 hints left