This commit is contained in:
parent
1fe0c770f9
commit
5ee734e9a5
1 changed files with 20 additions and 0 deletions
20
medium/delete_the_middle_node_of_a_linked_list.py
Normal file
20
medium/delete_the_middle_node_of_a_linked_list.py
Normal file
|
|
@ -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
|
||||||
Loading…
Add table
Add a link
Reference in a new issue