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)

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
Design Linked List INPUT Initial: Empty List NULL Operations: 1. addAtHead(1) 2. addAtTail(3) 3. addAtIndex(1, 2) 4. get(1) 5. deleteAtIndex(1) 6. get(1) Node Structure: val next ALGORITHM STEPS 1 addAtHead(1) Create node, point to head 1 --> NULL 2 addAtTail(3) Traverse to end, append [3] --> 1 --> 3 3 addAtIndex(1,2) Find index-1, insert node [2] --> [3] --> 1 --> 2 --> 3 4 get(1) = 2 Traverse to index, return val 5 deleteAtIndex(1) Unlink node, update ptrs [3] --> 1 --> 3 6 get(1) = 3 Index 1 now points to 3 FINAL RESULT Final Linked List State: head 1 idx:0 3 idx:1 NULL Output Array: [null,null,null,null,2,null,3] Results Breakdown: MyLinkedList() --> null addAtHead(1) --> null addAtTail(3) --> null addAtIndex(1,2) --> null get(1) --> 2 deleteAtIndex(1) --> null get(1) --> 3 Key Insight: Linked lists excel at O(1) insertions/deletions at known positions, but require O(n) traversal to find positions. Key operations: maintain head pointer, handle edge cases (empty list, invalid index), and properly update next pointers. For doubly linked lists, also maintain prev pointers and tail for O(1) tail operations. TutorialsPoint - Design Linked List | Optimal Solution
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