Skip to content

Latest commit

 

History

History
33 lines (24 loc) · 599 Bytes

876.md

File metadata and controls

33 lines (24 loc) · 599 Bytes

Middle of the Linked List

Description

link


Solution

  • See Code

Code

O(n)

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def middleNode(self, head: ListNode) -> ListNode:
        fast = head
        slow = head
        while fast.next and fast.next.next:
            slow = slow.next
            fast = fast.next.next
        return slow .next if fast.next else slow