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 atindex, or -1 if the index is invalid.addAtHead(val)/addAtTail(val)insert at the front / back.addAtIndex(index, val)inserts before the node atindex; ifindexequals the length, append; if it's
greater, do nothing.
deleteAtIndex(index)deletes the node atindexif it's valid.