JavaScript Notes
Master the Linked List data structure in JavaScript. Learn singly and doubly linked lists, node creation, insertions, deletions, traversal, and linked list interview questions with code examples.
A Linked List is a linear data structure where each element (called a node) stores its value and a pointer to the next node. Unlike arrays, linked lists do not store data in contiguous memory — nodes are scattered in memory and linked together like a chain.
💡 Key insight: Linked lists excel at O(1) insertion and deletion at any known position (no shifting). But they have O(n) search — you must traverse from the head.
Node Structure
Node and LinkedList Classes
"5 → 10 → 20 → 30 → null" "Length: 4"
Insert at a Specific Index
list.insertAt(2, 15);
console.log(list.toString());"5 → 10 → 15 → 20 → 30 → null"
Delete a Value
delete(value) {
if (!this.head) return null;
// Delete head
if (this.head.value === value) {
const removed = this.head;
this.head = this.head.next;
if (!this.head) this.tail = null;
this.length--;
return removed.value;
}
// Find node before the target
let current = this.head;
while (current.next) {
if (current.next.value === value) {
const removed = current.next;
current.next = removed.next;
if (!current.next) this.tail = current; // update tail
this.length--;
return removed.value;
}
current = current.next;
}
return null; // not found
}const list2 = new LinkedList();
list2.append(1).append(2).append(3).append(4);
console.log("Before:", list2.toString());
list2.delete(2);
console.log("After delete 2:", list2.toString());
list2.delete(1);
console.log("After delete 1:", list2.toString());"Before: 1 → 2 → 3 → 4 → null" "After delete 2: 1 → 3 → 4 → null" "After delete 1: 3 → 4 → null"
Reverse a Linked List
reverse() {
let prev = null;
let current = this.head;
this.tail = this.head; // old head becomes new tail
while (current) {
const next = current.next;
current.next = prev;
prev = current;
current = next;
}
this.head = prev;
return this;
}const list3 = new LinkedList();
list3.append(1).append(2).append(3).append(4).append(5);
console.log("Before reverse:", list3.toString());
list3.reverse();
console.log("After reverse:", list3.toString());"Before reverse: 1 → 2 → 3 → 4 → 5 → null" "After reverse: 5 → 4 → 3 → 2 → 1 → null"
Detect a Cycle (Floyd's Algorithm)
function hasCycle(head) {
let slow = head;
let fast = head;
while (fast && fast.next) {
slow = slow.next; // move 1 step
fast = fast.next.next; // move 2 steps
if (slow === fast) return true; // cycle detected
}
return false;
}
// Create a cycle: 1 → 2 → 3 → 4 → 2 (back to node 2)
const n1 = new Node(1), n2 = new Node(2), n3 = new Node(3), n4 = new Node(4);
n1.next = n2; n2.next = n3; n3.next = n4; n4.next = n2; // cycle!
console.log("Has cycle:", hasCycle(n1));
// Normal list
const m1 = new Node(1); m1.next = new Node(2);
console.log("Has cycle:", hasCycle(m1));"Has cycle: true" "Has cycle: false"
Time & Space Complexity
| Operation | Singly Linked List | Array |
|---|---|---|
| Prepend (insert head) | O(1) | O(n) (shift) |
| Append (insert tail, with tail ptr) | O(1) | O(1) amortised |
| Insert at index | O(n) traverse + O(1) insert | O(n) shift |
| Delete by value | O(n) | O(n) shift |
| Search by value | O(n) | O(n) linear / O(log n) binary |
| Access by index | O(n) | O(1) |
| Space | O(n) + pointer overhead | O(n) |
When to Use a Linked List
✅ Use a linked list when:
- You need frequent insertions/deletions at arbitrary positions
- You don't know the size in advance (dynamic growth)
- You're implementing a stack, queue, or deque
- Memory usage should grow/shrink precisely
❌ Don't use a linked list when:
- You need random access by index (arrays are O(1))
- The data is small (array overhead is negligible)
- Cache performance matters (arrays are cache-friendly; linked list nodes are scattered)
Common Mistakes
- Losing the reference to a node before rerouting pointers — always save
current.nextbefore overwriting it. - Not updating the
tailpointer — easy to forget when inserting/deleting the last node. - Infinite loop in traversal — if you accidentally create a cycle, a
while(current)loop never ends. Use a visited set or Floyd's algorithm to detect. - Off-by-one in index insertion — traverse to
index - 1, notindex.
Interview Questions
Q1. What is a linked list and how does it differ from an array?
A linked list is a chain of nodes where each node holds a value and a pointer to the next node. Arrays store data contiguously; linked lists scatter nodes in memory linked by pointers. Arrays are O(1) for index access; linked lists are O(n). Linked lists are O(1) for prepend/head insert; arrays are O(n) because they shift elements.
Q2. What is the difference between singly and doubly linked lists?
A singly linked list has each node pointing to the next node only (next). A doubly linked list has each node pointing to bothnextandprev. Doubly linked lists support O(1) deletion of a known node and bidirectional traversal, at the cost of extra memory per node.
Q3. How do you reverse a singly linked list in O(n) time and O(1) space?
Iterate with three pointers:prev = null,current = head,next. At each step: savenext = current.next, setcurrent.next = prev, advanceprev = current, advancecurrent = next. After the loop,previs the new head.
Q4. How do you detect a cycle in a linked list?
Use Floyd's cycle detection (fast and slow pointers). Moveslowone step andfasttwo steps per iteration. If they meet, there's a cycle. Iffastreaches null, there's no cycle. Time O(n), Space O(1).
Q5. How do you find the middle of a linked list in one pass?
Use two pointers: slow (1 step) and fast (2 steps). When fast reaches the end, slow is at the middle. ``js function findMiddle(head) { let slow = head, fast = head; while (fast && fast.next) { slow = slow.next; fast = fast.next.next; } return slow; } ``Q6. When would you prefer a linked list over an array?
When you need frequent insertions/deletions at arbitrary positions without the cost of shifting elements — e.g., implementing a text editor buffer, LRU cache, or OS process scheduler.
Q7. What is the time complexity of deleting a node if you only have a pointer to that node?
In a singly linked list, you still need O(n) to find the previous node. However, a trick exists: copy the value ofnode.nextintonode, then skipnode.next. This achieves O(1) but doesn't work for the last node.
Key Takeaways
- Each node holds a value and a next pointer (and
previn doubly linked lists). - Prepend is O(1) — no shifting needed, just update head pointer.
- Search is O(n) — must traverse from head (no index access).
- Key algorithms: reverse (3 pointers), cycle detection (fast/slow pointers), find middle (fast/slow pointers).
- Linked lists are ideal for dynamic-size structures with frequent insertions/deletions; arrays are better for random access and cache-friendly iteration.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Linked List in JavaScript — Nodes, Pointers & Operations Guide.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this JavaScript Master Course topic.
Search Terms
javascript-complete-guide, javascript master course, javascript, complete, guide, data, structures, linked
Related JavaScript Master Course Topics