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:
•
•
•
•
•
This problem tests your understanding of pointer manipulation, memory management, and edge case handling - fundamental skills for any software engineer!
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 indexThis 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)
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
Visualization
Tap to expand
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.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code