Last active
March 25, 2023 22:23
-
-
Save Khamies/489356d8b331a0ff1fe96b239048a7f4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_pointers_meetingPoint(head): | |
slow = head | |
fast = head | |
while fast and fast.next: | |
slow = slow.next | |
fast = fast.next.next | |
if slow == fast: | |
return slow | |
return None | |
def get_cycle_start(head, meeting_point): | |
p = head | |
q = meeting_point | |
while p != q: | |
p = p.next | |
q = q.next | |
return p | |
if __name__ == '__main__': | |
meeting_point = get_pointers_meetingPoint(head) | |
if not meeting_point: | |
return None | |
else: | |
return get_cycle_start(head, meeting_point) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment