AW Dev Rethought

βœ‚οΈ Perfection is achieved not when there is nothing more to add, but when there is nothing left to take away - Antoine de Saint-ExupΓ©ry

🧩 Python DSA Patterns β€” Linked List Patterns πŸ”—πŸ


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

  1. Reverse Linked List
  2. Linked List Cycle
  3. Middle of the Linked List
  4. Remove Nth Node From End
  5. 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


Link copied!

Comments

Add Your Comment

Comment Added!