AniUI Academy
medium+250 XPPractice

A singly linked list you can reverse

A linked list stores each value in a node that points at the next one. Nothing needs to shift when you insert at the head, which is why lists show up inside schedulers, undo chains and the internals of frameworks.

Implement a LinkedList class. It is constructed with no arguments and starts empty. append(value) adds a value at the tail and returns the new length. prepend(value) adds a value at the head and returns the new length. get(index) returns the value at a zero-based index, or undefined when the index is negative or past the end. removeAt(index) removes the node at that index and returns its value, or returns undefined when the index is out of range. size() returns the number of values. toArray() returns the values as a plain array from head to tail, and [] when empty. reverse() reverses the list in place; its return value is not used.

What it has to do

  • append and prepend return the length after the insertion.
  • get and removeAt return undefined for negative or out-of-range indices.
  • removeAt(0) works and keeps the rest of the list intact.
  • reverse() reverses in place and works on an empty list.
  • size() stays accurate after every insertion and removal.

Your workspace

Try it yourself
Loading playground...

Ready to check it?

5 tests run against your code, right here in your browser. Sign in to claim the XP when you pass.

AI Crack & Solution Assist

Stuck? Get instant AI hints or break down the optimal solution.

Stuck? The javascript course covers everything this challenge needs.