Back to DSA Topics
Linked Lists
Linked lists are linear data structures where elements are stored in nodes, each pointing to the next node.
Animated Visualization
Step 1 of 4
10
→
20
→
30
Initial linked list: Head → 10 → 20 → 30 → null
javascript
← Scroll →
// Node Classclass Node { constructor(data) { this.data = data; this.next = null; }}// Linked List Classclass LinkedList { constructor() { this.head = null; this.size = 0; } // Add element at end append(data) { const newNode = new Node(data); if (!this.head) { this.head = newNode; } else { let current = this.head; while (current.next) { current = current.next; } current.next = newNode; } this.size++; } // Add element at beginning prepend(data) { const newNode = new Node(data); newNode.next = this.head; this.head = newNode; this.size++; } // Delete element delete(data) { if (!this.head) return; if (this.head.data === data) { this.head = this.head.next; this.size--; return; } let current = this.head; while (current.next && current.next.data !== data) { current = current.next; } if (current.next) { current.next = current.next.next; this.size--; } } // Time Complexity: // Access: O(n) // Search: O(n) // Insertion: O(1) at head, O(n) at end // Deletion: O(n)Swipe horizontally to view full code
Explanation
Linked lists are dynamic data structures that don't require contiguous memory. Each node contains data and a reference to the next node. Insertion and deletion at the head are O(1), but accessing elements requires traversal (O(n)).
Operations & Complexity
AccessO(n)
Must traverse from head
SearchO(n)
Linear search through nodes
Insertion (head)O(1)
Direct insertion at head
Insertion (end)O(n)
Must traverse to end
DeletionO(n)
Must find node first
Time Complexity
AccessO(n)
Must traverse from head
SearchO(n)
Linear search through nodes
Insertion (head)O(1)
Direct insertion at head
Insertion (end)O(n)
Must traverse to end
DeletionO(n)
Must find node first