# 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: Optional[ListNode]) -> Optional[ListNode]:
slow= head
fast = head
while fast and fast.next:
fast = fast.next.next
slow = slow.next
return slow
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
fast = head
slow =head
meet =None
while fast and fast.next:
fast =fast.next.next
slow = slow.next
if fast == slow:
meet = fast
break
while head and meet and head != meet:
head = head.next
meet = meet.next
return meet