π§© Python DSA Patterns β Linked List Patterns ππ
Posted on: February 6, 2026
Description:
Linked list problems arenβt about indexes β theyβre about pointer movement.
Once you learn a few core techniques (fast/slow pointers, dummy nodes, in-place reversal), most linked list questions follow predictable patterns.
This post focuses on the most reusable linked list patterns youβll see again and again.
π§ Core Linked List Patterns
- In-place reversal (no extra memory)
- Fast & slow pointers (cycle, middle)
- Dummy nodes (safe deletions & merges)
- Two-pointer traversal (from both ends conceptually)
π Reverse a Linked List (In-Place)
Reverse links one by one using three pointers.
def reverse_list(head):
prev = None
curr = head
while curr:
nxt = curr.next
curr.next = prev
prev = curr
curr = nxt
return prev
π‘ Why it matters:
This pattern is reused in palindrome checks, k-group reversal, and list reordering.
π Detect Cycle (Fast & Slow Pointers)
If fast and slow pointers meet, a cycle exists.
def has_cycle(head):
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
return True
return False
π‘ Insight:
Fast pointer laps the slow pointer inside a loop.
π Find the Middle of a Linked List
Fast pointer moves twice as fast as slow.
def middle_node(head):
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow
π‘ Common use:
Split lists, binary tree conversion, palindrome checks.
π Remove Nth Node From End (Dummy Node Trick)
A dummy node avoids edge cases when removing the head.
def remove_nth_from_end(head, n):
dummy = ListNode(0, head)
slow = fast = dummy
for _ in range(n + 1):
fast = fast.next
while fast:
slow = slow.next
fast = fast.next
slow.next = slow.next.next
return dummy.next
π‘ Why dummy helps:
It guarantees a safe previous node for deletion.
π Merge Two Sorted Linked Lists
Merge without creating new nodes.
def merge_two_lists(l1, l2):
dummy = ListNode()
tail = dummy
while l1 and l2:
if l1.val < l2.val:
tail.next = l1
l1 = l1.next
else:
tail.next = l2
l2 = l2.next
tail = tail.next
tail.next = l1 or l2
return dummy.next
π‘ Pattern:
Build result using pointer rewiring, not extra memory.
π§ Where Linked List Patterns Are Used
- π Reversal & reordering problems
- π Cycle detection
- π§© Palindrome linked lists
- π Merge & split operations
- π§ Pointer-based optimizations
β Takeaway
- Linked list solutions rely on pointer control
- Fast/slow pointers solve cycle & middle problems
- Dummy nodes simplify deletion logic
- Most problems run in O(n) time, O(1) space
Once these patterns click, linked list problems become mechanical.
ποΈ Practice Problems
- Reverse Linked List
- Linked List Cycle
- Middle of the Linked List
- Remove Nth Node From End
- Merge Two Sorted Lists
π Full Script
The blog covers the essentials β find the complete notebook with all snippets & extras on GitHub Repo π Python DSA Patterns
No comments yet. Be the first to comment!