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 = val
6
# self.next = next
7
class Solution:
8
def deleteMiddle(self, head: Optional[ListNode]) -> Optional[ListNode]:
9
if not head:
10
return head
11
if head and not head.next:
12
return None
13

14
prev = ListNode(0, head)
15
slow = fast = head
16
while fast and fast.next:
17
prev = slow
18
slow = slow.next
19
fast = fast.next.next
20

21
prev.next = slow.next
22
return head

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0