πŸ“˜ Singly Linked List – Basics & Problem-Solving Guide

Complete guide to singly linked lists, fundamental operations, patterns, and interview strategies.


Table of Contents

Part 1: Fundamentals

Part 2: Operations & Patterns

Part 3: Problem-Solving

Part 4: Interview Guide


1. What Is a Linked List?

A linked list is a linear data structure where each element (node) contains:

Nodes are not stored contiguously in memory.

1.1 Singly Linked List Node

struct ListNode {
    int val;
    ListNode* next;
};

2. Why Linked Lists?

Feature Array Linked List
Memory Contiguous Non-contiguous
Access O(1) random O(n) sequential
Insertion Costly O(1) (if pointer known)
Size Fixed Dynamic

πŸ‘‰ Linked lists shine when frequent insertions/deletions are needed.


3. Core Terminology


4. Fundamental Operations

4.1 Traverse a Linked List

ListNode* curr = head;
while (curr != nullptr) {
    cout << curr->val << " ";
    curr = curr->next;
}

4.2 Insert at Head

ListNode* insertAtHead(ListNode* head, int val) {
    ListNode* node = new ListNode(val);
    node->next = head;
    return node;
}

4.3 Insert at Tail

ListNode* insertAtTail(ListNode* head, int val) {
    ListNode* node = new ListNode(val);
    if (!head) return node;

    ListNode* curr = head;
    while (curr->next) curr = curr->next;
    curr->next = node;
    return head;
}

4.4 Delete Head

ListNode* deleteHead(ListNode* head) {
    if (!head) return nullptr;
    ListNode* temp = head;
    head = head->next;
    delete temp;
    return head;
}

4.5 Delete by Value

ListNode* deleteByValue(ListNode* head, int val) {
    if (!head) return nullptr;
    if (head->val == val) return deleteHead(head);

    ListNode* curr = head;
    while (curr->next && curr->next->val != val)
        curr = curr->next;

    if (curr->next) {
        ListNode* temp = curr->next;
        curr->next = temp->next;
        delete temp;
    }
    return head;
}

5. Golden Rules for Linked Lists

πŸ”‘ Rule 1: Never Lose the Head

Always keep a pointer to the head. If you lose it, the entire list becomes inaccessible.

πŸ”‘ Rule 2: Before Accessing

Access Condition
node->val node != nullptr
node->next node != nullptr
node->next->next node && node->next

πŸ”‘ Rule 3: Dummy Node Pattern

Use dummy nodes to simplify edge cases.

ListNode dummy;
dummy.next = head;
ListNode* curr = &dummy;

Used in:


6. How to Approach Any Linked List Problem

βœ… Step-by-Step Strategy

Step 1: Identify the Pattern

Ask yourself:

Step 2: Handle Base Cases First

if (!head || !head->next) return head;

Step 3: Draw the List (Mentally or on Paper)

Visualize:

1 β†’ 2 β†’ 3 β†’ 4 β†’ NULL

Track pointer movement step-by-step.

Step 4: Move Pointers Carefully

Update pointers before losing access:

next = curr->next;
curr->next = prev;

Step 5: Return the Correct Node


7. Most Important Patterns

πŸ” Reversal Pattern

Used in:

πŸ’πŸ‡ Slow–Fast Pointer (Floyd’s Algorithm)

Used in:

πŸ”— Merge Pattern

Used in:


8. Common Mistakes to Avoid

❌ Accessing next without null check
❌ Forgetting to break links
❌ Losing head pointer
❌ Returning wrong node
❌ Mixing object & pointer (ListNode vs ListNode*)


9. Practice Order (Highly Recommended)

Beginner

Intermediate

Advanced


10. Interview Mindset Tip

When stuck:

β€œLet me use a dummy node to simplify edge cases.”

This line alone shows strong linked list maturity to interviewers.



Ready to Practice?

Test your understanding with curated problems from top platforms

πŸ“ Practice Problems

← Back to Linked List DSA Hub πŸ