Linked List Algorithms
Singly and doubly linked lists, pointers
Linked List - Complete Guide
Linked List: Master the Fundamentals
Linked lists are a fundamental data structure where elements are connected via pointers. They appear frequently in coding interviews and form the basis for more complex structures like trees and graphs.
This guide covers the main linked-list patterns and concepts. Each concept links to a detailed page with explanations and practice problems.
What Is a Linked List?
A singly linked list is a sequence of nodes; each node holds a value and a next pointer. A doubly linked list adds a prev pointer. Unlike arrays, there is no O(1) index accessβyou traverse by following pointers.
Key characteristics:
- Sequential access β traverse from head (and optionally tail in doubly linked)
- Dynamic size β no fixed capacity
- Pointer manipulation β insert/delete by rewiring
next(andprev) - No random access β finding the k-th node is O(k)
Core Techniques
1. Dummy Head / Sentinel Node
Using a dummy head (sentinel node) before the real head simplifies edge cases: no special handling for empty list or operations at the head.
Learn more: Dummy Head / Sentinel Node β
2. Fast and Slow Pointers (Floyd)
Fast and slow pointers (e.g. one moves 1 step, the other 2) are used for cycle detection, finding the middle node, and similar problems.
Learn more: Fast and Slow Pointers (Floyd) β
3. Reversing a Linked List (In-Place)
Reverse in-place by rewiring next pointers as you traverse. A classic interview problem and building block for others.
Learn more: Reversing a Linked List (In-Place) β
4. Merging Two Sorted Lists
Merge two sorted lists into one sorted list by comparing heads and advancing. Foundation for merge sort on linked lists.
Learn more: Merging Two Sorted Lists β
5. Cycle Detection (Floyd's Algorithm)
Cycle detection determines if a list has a cycle using fast/slow pointers (Floyd's cycle-finding algorithm).
Learn more: Cycle Detection (Floyd's Algorithm) β
6. Finding the Middle Node
Find the middle using slow (1 step) and fast (2 steps) pointers; when fast reaches the end, slow is at the middle.
Learn more: Finding the Middle Node β
7. Remove Nth Node From End
Remove the nth node from the end in one pass using two pointers: advance one by n steps, then move both until the leading pointer reaches the end.
Learn more: Remove Nth Node From End β
8. Intersection of Two Linked Lists
Intersection of two lists β find the first common node (if any). Approaches include hashing or aligning lengths then stepping together.
Learn more: Intersection of Two Linked Lists β
9. Doubly Linked List
Doubly linked lists have prev and next, enabling O(1) deletion of a known node and easier reverse traversal.
Learn more: Doubly Linked List β
Pattern-Based Problems
10. Palindrome Linked List
Palindrome linked list β check if the list reads the same forward and backward. Often solved by finding the middle, reversing the second half, and comparing.
Learn more: Palindrome Linked List β
11. Add Two Numbers (List Representation)
Add two numbers represented as linked lists (digits in reverse order). Simulate digit-by-digit addition with a carry.
Learn more: Add Two Numbers (List Representation) β
12. Reorder List (Half Reverse, Merge)
Reorder list β e.g. L0 β Ln β L1 β Lnβ1 β β¦ Combine finding the middle, reversing the second half, and merging two lists.
Learn more: Reorder List (Half Reverse, Merge) β
13. Swap Nodes in Pairs
Swap nodes in pairs β swap every two adjacent nodes. Use a dummy head and careful pointer updates.
Learn more: Swap Nodes in Pairs β
14. Reverse Nodes in K-Group
Reverse nodes in k-group β reverse every k consecutive nodes. Extend the in-place reverse pattern to fixed-size segments.
Learn more: Reverse Nodes in K-Group β
15. Merge K Sorted Lists
Merge k sorted lists into one. Use a min-heap of head nodes or merge in pairs (divide and conquer).
Learn more: Merge K Sorted Lists β
16. Copy List with Random Pointer
Copy list with random pointer β deep copy a list where each node has an extra random pointer. Typically done with a map or in-place interleaving.
Learn more: Copy List with Random Pointer β
17. Partition List
Partition list β rearrange so all nodes less than a value come before nodes greater than or equal (e.g. quick sort partition).
Learn more: Partition List β
18. Sort List (Merge Sort on List)
Sort list in O(n log n) time and O(1) space (excluding recursion). Use merge sort: find middle, sort halves, merge.
Learn more: Sort List (Merge Sort on List) β
19. Odd Even Linked List
Odd even linked list β group nodes at odd indices then even indices. Single pass with two dummy heads.
Learn more: Odd Even Linked List β
20. Delete Node Without Head Reference
Delete node when you don't have the head β copy the next node's value into the current node and skip the next node.
Learn more: Delete Node Without Head Reference β
21. Flatten a Multilevel Doubly Linked List
Flatten multilevel doubly linked list β flatten a list that has child pointers so the result is a single-level doubly linked list.
Learn more: Flatten a Multilevel Doubly Linked List β
22. Design Linked List (Implementation)
Design linked list β implement get, addAtHead, addAtTail, addAtIndex, deleteAtIndex. Good practice for pointer handling and edge cases.
Learn more: Design Linked List (Implementation) β
Learning Path
Start with: Dummy Head, Reverse In-Place, Fast and Slow Pointers.
Then: Merge Two Sorted, Cycle Detection, Find Middle, Remove Nth From End.
Advanced: Reorder List, Reverse Nodes in K-Group, Sort List, Copy List with Random Pointer.
Summary
Linked list problems rely on dummy head, fast/slow pointers, in-place reverse, and merge patterns. Use the concept links above to dive into each topic and practice.