2095. Delete the Middle Node of a Linked List
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 60% Topics: Linked List, Two Pointers
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(1)
# Definition for singly-linked list.
class ListNode(object):
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution(object):
def deleteMiddle(self, head):
"""
:type head: Optional[ListNode]
:rtype: Optional[ListNode]
"""
dummy = ListNode()
dummy.next = head
slow = fast = dummy
while fast.next and fast.next.next:
slow, fast = slow.next, fast.next.next
slow.next = slow.next.next
return dummy.next
Solution from kamyu104/LeetCode-Solutions · MIT