Back to June Back to Year 2026
MEDIUM 15 Jun 2026 View on LeetCode

2095. Delete the Middle Node of a Linked List

</> Solution

/**
 * Problem: LeetCode 2095 - Delete the Middle Node of a Linked List
 * Difficulty: Medium
 * Language: Java
 * Author: CodeByTushu
 */
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
/**
 * Problem: LeetCode 2095 - Delete the Middle Node of a Linked List
 * Difficulty: Medium
 * Language: Java
 * Author: CodeByTushu
 */
class Solution {
    public ListNode deleteMiddle(ListNode head) {
       // If there is only one node
        if (head == null || head.next == null) {
            return null;
        }

        ListNode slow = head;
        ListNode fast = head;
        ListNode prev = null;

        // Find the middle node
        while (fast != null && fast.next != null) {
            prev = slow;
            slow = slow.next;
            fast = fast.next.next;
        }

        // Delete the middle node
        prev.next = slow.next;

        return head; 
    }
}

TIME COMPLEXITY

O(N)

SPACE COMPLEXITY

O(1)

TOPICS

Linked List Two Pointers