Design Linked List - Problem
Design your own linked list data structure! Your task is to implement a fully functional linked list with all the essential operations.

You can choose between a singly linked list (each node points to the next) or a doubly linked list (each node has both next and previous pointers). All nodes are 0-indexed.

Your MyLinkedList class must support:
get(index) - Retrieve value at given index (return -1 if invalid)
addAtHead(val) - Insert new node at the beginning
addAtTail(val) - Append new node at the end
addAtIndex(index, val) - Insert node before given index
deleteAtIndex(index) - Remove node at given index

This problem tests your understanding of pointer manipulation, memory management, and edge case handling - fundamental skills for any software engineer!

Input & Output

example_1.py — Basic Operations
$ Input: ["MyLinkedList", "addAtHead", "addAtTail", "addAtIndex", "get", "deleteAtIndex", "get"] [[], [1], [3], [1, 2], [1], [1], [1]]
Output: [null, null, null, null, 2, null, 3]
💡 Note: Initialize empty list → Add 1 at head: [1] → Add 3 at tail: [1,3] → Add 2 at index 1: [1,2,3] → Get index 1: returns 2 → Delete index 1: [1,3] → Get index 1: returns 3
example_2.py — Edge Cases
$ Input: ["MyLinkedList", "addAtIndex", "addAtIndex", "addAtIndex", "get"] [[], [0, 10], [0, 20], [1, 30], [0]]
Output: [null, null, null, null, 20]
💡 Note: Add 10 at index 0: [10] → Add 20 at index 0: [20,10] → Add 30 at index 1: [20,30,10] → Get index 0: returns 20
example_3.py — Invalid Operations
$ Input: ["MyLinkedList", "get", "addAtIndex", "get", "get", "addAtIndex"] [[], [0], [0, 1], [0], [1], [2, 2]]
Output: [null, -1, null, 1, -1, null]
💡 Note: Get from empty list returns -1 → Add 1 at index 0: [1] → Get index 0: returns 1 → Get index 1: returns -1 (out of bounds) → Add at index 2: no change (index too large)

Visualization

Tap to expand
Linked List Design Evolution1. Basic Singly Linked122. With Dummy Headdummy13. Doubly Linked124. Optimized (Both Dummy)head1tailBenefits of Evolution:✓ Dummy nodes eliminate edge cases✓ Bidirectional traversal for efficiency✓ O(1) operations at both ends✓ Smart traversal chooses shorter path
Understanding the Visualization
1
Singly Linked List
Basic implementation with head pointer only
2
Add Dummy Head
Eliminates special cases for head operations
3
Doubly Linked
Add prev pointers for bidirectional traversal
4
Dummy Tail + Optimization
Add dummy tail and smart traversal for optimal performance
Key Takeaway
🎯 Key Insight: Using dummy nodes and bidirectional pointers transforms a simple linked list into a highly efficient data structure with uniform operation handling.

Time & Space Complexity

Time Complexity
⏱️
O(min(index, n-index))

Can traverse from either end, choosing the shorter path to the target

n
2n
Linear Growth
Space Complexity
O(1)

Constant extra space for operations, regardless of list size

n
2n
Linear Space

Constraints

  • 0 ≤ index, val ≤ 1000
  • Please do not use the built-in LinkedList library
  • At most 2000 calls will be made to get, addAtHead, addAtTail, addAtIndex, and deleteAtIndex
Asked in
Amazon 45 Microsoft 38 Google 32 Meta 28
89.5K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen