2095. Delete the Middle Node of a Linked List

2095. Delete the Middle Node of a Linked List

# 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