Design Linked List

Medium · Linked Lists · about 25 minutes

Asked at Amazon, Microsoft, Google, Apple.

Implement a singly linked list (0-indexed) without using a built-in list type.

  • MyLinkedList() creates an empty list.
  • get(index) returns the value at index, or -1 if the index is invalid.
  • addAtHead(val) / addAtTail(val) insert at the front / back.
  • addAtIndex(index, val) inserts before the node at index; if index equals the length, append; if it's

greater, do nothing.

  • deleteAtIndex(index) deletes the node at index if it's valid.