Delete the Middle Node of a Linked List LeetCode solution in Python
1# Runtime: 2948 ms (Top 45.50%) | Memory: 60.7 MB (Top 40.45%)2# Definition for singly-linked list.3# class ListNode:4# def __init__(self, val=0, next=None):5# self.val = val6# self.next = next7class Solution:8def deleteMiddle(self, head: Optional[ListNode]) -> Optional[ListNode]:9if not head:10return head11if head and not head.next:12return None1314prev = ListNode(0, head)15slow = fast = head16while fast and fast.next:17prev = slow18slow = slow.next19fast = fast.next.next2021prev.next = slow.next22return head