← Back to challenges

Implementation Linked List

JavaScriptHardarraysdata_structures

Instructions

Implement a LinkedList function.

Method to Implementation

  • addFirst(n): Add an item to the head of the list.
  • addLast(n): Add an item to the tail of the list.
  • deleteFirst(): delete the head of the list.
  • deleteLast(): remove the tail of the list
  • contains(value): return a boolean as to whether a particular value is in the list
  • indexOf(value): return the "index" of the passed-in value.

Note that all inputs will be valid. That is, deleteFirst and deleteLast will never be called on an empty list, and the value searched for with indexOf will always exist within the given list.

Examples

list.addFirst(10) ➞ "[10]"

list.addLast(20) ➞ "[10, 20]"

list.addFirst(30) ➞ "[30, 10, 20]"

list.deleteLast() ➞ "[10, 20, 30]"

Notes

  • Pay special attention to the toString() function that's supplied in the test tab! Your submission will fail if you don't.
  • Feel free to write this as an ES6 class instead.
javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.