Linked List January 28 ,2026

Middle of a Linked List

Problem Statement

Given a singly linked list, find and return the middle node of the linked list.

Rules

  • If the number of nodes is odd, return the exact middle node.
  • If the number of nodes is even, return the second middle node (this is the standard convention followed in most interviews and coding platforms).

Understanding the Problem

Example 1 (Odd Length)

Input:

1 → 2 → 3 → 4 → 5 → NULL

Output:

3

Example 2 (Even Length)

Input:

10 → 20 → 30 → 40 → NULL

Output:

30

Explanation:
There are two middle nodes (20 and 30). We return the second middle node.

Important Edge Cases

  1. Empty linked list → no middle node
  2. Linked list with one node → that node is the middle
  3. Linked list with two nodes → return the second node

Handling edge cases is crucial in interviews.

Approach 1: Two-Pointer Technique (Slow and Fast Pointer)

Core Idea (Most Important)

This is the optimal and most widely accepted approach.

  • Use two pointers:
    • slow moves one step at a time
    • fast moves two steps at a time
  • When fast reaches the end:
    • slow will be at the middle

Why This Works

  • Fast pointer travels twice as fast
  • When fast finishes the list, slow has covered half the distance

Algorithm

  1. Initialize two pointers slow and fast to head
  2. While fast and fast.next are not NULL:
    • Move slow by one node
    • Move fast by two nodes
  3. Return slow

Time and Space Complexity

  • Time Complexity: O(n)
  • Space Complexity: O(1)

Implementation (All Languages)

C Implementation


// While running this code, replace all #include "..." with #include <...>

#include "stdio.h"
#include "stdlib.h"

struct Node {
    int data;
    struct Node* next;
};

/* Function to find middle using slow-fast pointers */
struct Node* findMiddle(struct Node* head) {
    if (head == NULL)
        return NULL;

    struct Node* slow = head;
    struct Node* fast = head;

    while (fast != NULL && fast->next != NULL) {
        slow = slow->next;
        fast = fast->next->next;
    }

    return slow;  // second middle in even case
}

/* Helper to insert node at end */
struct Node* insert(struct Node* head, int data) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = data;
    newNode->next = NULL;

    if (head == NULL) return newNode;

    struct Node* temp = head;
    while (temp->next != NULL)
        temp = temp->next;

    temp->next = newNode;
    return head;
}

/* Driver Code */
int main() {
    struct Node* head = NULL;

    // Example: 10 → 20 → 30 → 40
    head = insert(head, 10);
    head = insert(head, 20);
    head = insert(head, 30);
    head = insert(head, 40);

    struct Node* mid = findMiddle(head);

    if (mid != NULL)
        printf("Middle Node: %d\n", mid->data);
    else
        printf("List is empty\n");

    return 0;
}

Java Implementation

class Node {
    int data;
    Node next;

    Node(int data) {
        this.data = data;
        this.next = null;
    }
}

public class MiddleOfLinkedList {

    public static Node findMiddle(Node head) {
        if (head == null)
            return null;

        Node slow = head;
        Node fast = head;

        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }
}

Python Implementation

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

def find_middle(head):
    if head is None:
        return None

    slow = head
    fast = head

    while fast and fast.next:
        slow = slow.next
        fast = fast.next.next

    return slow

Dry Run (Very Important for Students)

Input

1 → 2 → 3 → 4 → 5 → NULL

Pointer Movement

StepSlowFast
Start11
123
235
Stop3NULL

Result

Middle node = 3

Approach 2: Counting Nodes (Brute Force Method)

This approach finds the middle of the linked list by counting the total number of nodes first, and then traversing the list again to reach the middle position.

Although this method is less efficient than the two-pointer technique, it is very useful for beginners and helps build a strong understanding of linked list traversal.

Algorithm

  1. If the linked list is empty, return NULL.
  2. Traverse the list and count the total number of nodes (n).
  3. Calculate the middle position:
    • middle = (n / 2) + 1
  4. Traverse the list again up to the middle position.
  5. Return the node at that position.

For even-length lists, this formula returns the second middle node, which matches standard interview conventions.

Time and Space Complexity

  • Time Complexity: O(n)
  • Space Complexity: O(1)
  • Traversals Required: 2

Implementation

C Implementation 

// While running this code, replace all #include "..." with #include <...>

#include "stdio.h"
#include "stdlib.h"

struct Node {
    int data;
    struct Node* next;
};

/* Function to find middle by counting nodes */
struct Node* findMiddleByCounting(struct Node* head) {
    if (head == NULL)
        return NULL;

    int count = 0;
    struct Node* temp = head;

    // First traversal (count nodes)
    while (temp != NULL) {
        count++;
        temp = temp->next;
    }

    int middle = (count / 2) + 1;

    // Second traversal
    temp = head;
    for (int i = 1; i < middle; i++) {
        temp = temp->next;
    }

    return temp;
}

/* Helper to insert node */
struct Node* insert(struct Node* head, int data) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = data;
    newNode->next = NULL;

    if (head == NULL) return newNode;

    struct Node* temp = head;
    while (temp->next != NULL)
        temp = temp->next;

    temp->next = newNode;
    return head;
}

/* Driver Code */
int main() {
    struct Node* head = NULL;

    // Example: 10 → 20 → 30 → 40
    head = insert(head, 10);
    head = insert(head, 20);
    head = insert(head, 30);
    head = insert(head, 40);

    struct Node* mid = findMiddleByCounting(head);

    if (mid != NULL)
        printf("Middle Node: %d\n", mid->data);
    else
        printf("List is empty\n");

    return 0;
}

Java Implementation 

class Node {
    int data;
    Node next;

    Node(int data) {
        this.data = data;
        this.next = null;
    }
}

public class MiddleOfLinkedListCount {

    public static Node findMiddleByCounting(Node head) {
        if (head == null)
            return null;

        int count = 0;
        Node temp = head;

        // First traversal
        while (temp != null) {
            count++;
            temp = temp.next;
        }

        int middle = (count / 2) + 1;

        // Second traversal
        temp = head;
        for (int i = 1; i < middle; i++) {
            temp = temp.next;
        }

        return temp;
    }
}

Python Implementation 

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

def find_middle_by_counting(head):
    if head is None:
        return None

    count = 0
    temp = head

    # First traversal to count nodes
    while temp:
        count += 1
        temp = temp.next

    middle = (count // 2) + 1

    # Second traversal to reach middle
    temp = head
    for _ in range(1, middle):
        temp = temp.next

    return temp

Dry Run 

Input

10 → 20 → 30 → 40 → NULL

Step 1: Count Nodes

Total nodes = 4

Step 2: Calculate Middle

middle = (4 / 2) + 1 = 3

Step 3: Traverse to Position 3

Node = 30

Comparison of Approaches

ApproachTimeSpaceTraversals
Two PointerO(n)O(1)1
CountingO(n)O(1)2

Common Mistakes Students Make

  1. Returning first middle in even-length list (wrong as per standard)
  2. Incorrect while condition
  3. Not checking fast.next
  4. Forgetting empty list case
  5. Infinite loop due to pointer misplacement

Interview Perspective

  • This problem tests:
    • Pointer manipulation
    • Loop conditions
    • Understanding of linked list traversal
  • Interviewers expect the two-pointer approach
  • Always explain why slow-fast works

Variations of This Problem

Practice these next:

  • Find first middle node
  • Find middle of circular linked list
  • Print both middle nodes
  • Delete middle node
  • Find middle in doubly linked list

Summary

  • The two-pointer approach is optimal and elegant
  • Works in one traversal
  • No extra space required
  • A foundational problem for advanced linked list questions

Mastering this problem makes solving cycle detection, palindrome check, and reversal problems much easier.


Next Problem in the Series

 Count Occurrences in a Linked List

Sanjiv
0

You must logged in to post comments.

Related Blogs

Introducti...
Linked List January 01 ,2026

Introduction to Link...

Length of...
Linked List January 01 ,2026

Length of a Singly L...

Remove Eve...
Linked List January 01 ,2026

Remove Every K-th No...

Count Occu...
Linked List January 01 ,2026

Count Occurrences in...

Circular L...
Linked List January 01 ,2026

Circular Linked List...

Check if a...
Linked List January 01 ,2026

Check if a Linked Li...

Count Node...
Linked List January 01 ,2026

Count Nodes in a Cir...

Deletion f...
Linked List January 01 ,2026

Deletion from a Circ...

Singly to...
Linked List January 01 ,2026

Singly to Circular L...

Exchange F...
Linked List January 01 ,2026

Exchange First and L...

Deletion i...
Linked List January 01 ,2026

Deletion in a Doubly...

Print a Si...
Linked List February 02 ,2026

Print a Singly Linke...

Search in...
Linked List February 02 ,2026

Search in a Singly L...

Linked Lis...
Linked List February 02 ,2026

Linked List Insertio...

Deleting a...
Linked List February 02 ,2026

Deleting a Given Key...

Deleting a...
Linked List February 02 ,2026

Deleting a Node at a...

Delete Ent...
Linked List February 02 ,2026

Delete Entire Linked...

Nth Node f...
Linked List February 02 ,2026

Nth Node from Start

Nth Node f...
Linked List February 02 ,2026

Nth Node from End

Size of Do...
Linked List February 02 ,2026

Size of Doubly Linke...

Reverse a...
Linked List February 02 ,2026

Reverse a Singly Lin...

Reverse a...
Linked List February 02 ,2026

Reverse a Doubly Lin...

Insert at...
Linked List February 02 ,2026

Insert at Beginning...

Insert at...
Linked List February 02 ,2026

Insert at End of Dou...

Delete Fir...
Linked List February 02 ,2026

Delete First Node of...

Check if L...
Linked List February 02 ,2026

Check if Linked List...

Copy a Lin...
Linked List February 02 ,2026

Copy a Linked List

Swap Nodes...
Linked List February 02 ,2026

Swap Nodes in Pairs

Detect Loo...
Linked List February 02 ,2026

Detect Loop in a Lin...

Length of...
Linked List February 02 ,2026

Length of Loop in Li...

Design Bro...
Linked List February 02 ,2026

Design Browser Histo...

Get In Touch

Kurki bazar Uttar Pradesh

+91-8808946970

techiefreak87@gmail.com