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): Walkindexsteps fromheadand return the node's value. Ifindex < 0orindex >= length, returnnull/None.insertAt(index, val):- If
index === 0, delegate toprepend(val). - If
index === length, delegate toappend(val). - Otherwise, traverse to
index - 1and rewireprev.nextto insert the new node. - Return
trueon success, orfalseif index is out of bounds.
- If
Your build
Running is free — Submit is what records the step. Or press ⌘↩
Tests
1 case| call | type | expected | result |
|---|---|---|---|
| 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