π§± 1. What is a Linked List?
A Linked List is a linear data structure where elements are stored in nodes, and each node points to the next.
[value | next] β [value | next] β [value | null]
π Last node always points to null
π§ 2. Node Structure
function Node(value) {
this.data = value;
this.next = null;
}
ποΈ 3. Linked List Class
var MyLinkedList = function () {
this.head = null;
this.len = 0; // MUST be 0
};
β οΈ IMPORTANT RULES (DO NOT IGNORE)
- Always use 0-based indexing
- Always update
len - Never break pointer chain
- Always check invalid index
π 4. Add At Head
πΌοΈ Visualization
π‘ Idea
New node becomes the first node.
πͺ Steps
- Create new node
- Point it to current head
- Move head to new node
β Code
MyLinkedList.prototype.addAtHead = function (val) {
const node = new Node(val);
node.next = this.head;
this.head = node;
this.len++;
};
β± Complexity
- O(1)
π 5. Add At Tail
πΌοΈ Visualization
π‘ Idea
Go to last node β attach new node
πͺ Steps
- If empty β head = node
- Else β traverse to last
- Connect last.next = node
β Code
MyLinkedList.prototype.addAtTail = function (val) {
const node = new Node(val);
if (!this.head) {
this.head = node;
this.len++;
return;
}
let current = this.head;
while (current.next) {
current = current.next;
}
current.next = node;
this.len++;
};
β± Complexity
- O(n)
π 6. Get Value at Index
πΌοΈ Visualization
π‘ Idea
Traverse from head to index
πͺ Steps
- Check bounds
- Move step-by-step
- Return value
β Code
MyLinkedList.prototype.get = function (index) {
if (index < 0 || index >= this.len) return -1;
let current = this.head;
for (let i = 0; i < index; i++) {
current = current.next;
}
return current.data;
};
β± Complexity
- O(n)
π 7. Add At Index
πΌοΈ Visualization
π‘ Idea
Reach index - 1, then insert
πͺ Steps
- If index invalid β return
- If index = 0 β head
- If index = len β tail
- Else:
- go to index - 1
- insert node
β Code
MyLinkedList.prototype.addAtIndex = function (index, val) {
if (index < 0 || index > this.len) return;
if (index === 0) {
this.addAtHead(val);
return;
}
if (index === this.len) {
this.addAtTail(val);
return;
}
let prev = this.head;
for (let i = 0; i < index - 1; i++) {
prev = prev.next;
}
const node = new Node(val);
node.next = prev.next;
prev.next = node;
this.len++;
};
β± Complexity
- O(n)
π Delete Node at Index in Linked List (Detailed)
π§ Core Idea
A linked list is a chain of nodes:
10 β 20 β 30 β 40
Each node stores:
value-
next(pointer to next node)
π To delete a node, you donβt remove it directly
π You change links (pointers) so it gets skipped
π― Goal
Delete node at a given index
Example:
Index: 2
Before:
10 β 20 β 30 β 40
After:
10 β 20 β 40
π‘ Key Insight
π You must stop at the previous node
Because:
prev.next = prev.next.next
This skips the node you want to delete.
πͺ Step-by-Step Explanation
β Step 1: Validate Index
if (index < 0 || index >= this.len) return;
Why?
- Negative index β
- Index beyond list size β
β Step 2: Special Case (index = 0)
Deleting head:
Before:
[10] β 20 β 30
After:
20 β 30
Code:
this.head = this.head.next;
π We simply move the head forward
β Step 3: Traverse to (index - 1)
Why?
Because we need the previous node
let prev = this.head;
for (let i = 0; i < index - 1; i++) {
prev = prev.next;
}
β Step 4: Bypass the Node
prev β toDelete β nextNode
becomes
prev β nextNode
Code:
let toDelete = prev.next;
prev.next = toDelete.next;
β Step 5: Cleanup (Optional but Good Practice)
toDelete.next = null;
π Helps garbage collection
π Avoids accidental memory reference
β Step 6: Update Length
this.len--;
π₯ Full Code (Clean Version)
MyLinkedList.prototype.deleteAtIndex = function (index) {
if (index < 0 || index >= this.len) return;
if (index === 0) {
this.head = this.head.next;
this.len--;
return;
}
let prev = this.head;
for (let i = 0; i < index - 1; i++) {
prev = prev.next;
}
let toDelete = prev.next;
prev.next = toDelete.next;
toDelete.next = null;
this.len--;
};
π§ͺ Dry Run Example
Initial list:
20 β 99 β 10 β 30
Delete index = 1
Step 1:
Go to index - 1 = 0
prev = 20
Step 2:
toDelete = 99
Step 3:
20.next = 10
Final list:
20 β 10 β 30
β± Time & Space Complexity
β± Time:
- Traversal β O(n)
π¦ Space:
- No extra memory β O(1)
π§ Ultimate Pattern to Remember
Delete = Skip node
prev.next = prev.next.next
π₯ FULL WORKING EXAMPLE
var obj = new MyLinkedList();
obj.addAtHead(10);
obj.addAtHead(20);
obj.addAtTail(30);
obj.addAtIndex(1, 99);
console.log(obj.get(1)); // 99
obj.deleteAtIndex(1);
console.log(obj.get(1)); // 10
π§ MONSTER LEVEL UNDERSTANDING
π Core Pattern
π Always work with previous node
prev.next = prev.next.next;
π Golden Rule
Linked List = Pointer manipulation, not value shifting
π Why Array vs Linked List
| Feature | Array | Linked List |
|---|---|---|
| Insert | O(n) | O(1) (if pointer known) |
| Delete | O(n) | O(1) |
| Access | O(1) | O(n) |
π Most Important Interview Insight
You NEVER delete node directly
You only change links








Top comments (0)