diff --git a/medium/delete_the_middle_node_of_a_linked_list.py b/medium/delete_the_middle_node_of_a_linked_list.py new file mode 100644 index 0000000..16e2913 --- /dev/null +++ b/medium/delete_the_middle_node_of_a_linked_list.py @@ -0,0 +1,20 @@ +# https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list + +from typing import Optional + +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]: + if head is None or head.next is None: + return None + slow = head + fast = head.next.next + while fast is not None and fast.next is not None: + slow = slow.next + fast = fast.next.next + slow.next = slow.next.next + return head \ No newline at end of file