---
title: "2095. Delete the Middle Node of a Linked List"
url: "https://laigary.com/interview/coding/2095-delete-the-middle-node-of-a-linked-list"
type: "note"
section: "coding"
date: "2024-03-09"
updated: "2024-03-09"
tags: ["Linked List", "Two Pointers"]
---

# 2095. Delete the Middle Node of a Linked List

[2095\. Delete the Middle Node of a Linked List](https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/)

```python
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def deleteMiddle(self, head: Optional[ListNode]) -> Optional[ListNode]:
        slow = head
        fast = head

        mid = 0
        while fast.next:
            if fast.next.next:
                fast = fast.next.next
            else:
                fast = fast.next
            mid += 1    
            slow = slow.next
        
        if mid == 0:
            return None
        
        prev = head
        while mid > 1:
            prev = prev.next
            mid -= 1
        prev.next = slow.next

        return head

```
