From 58c9345dca476ee09de6e919b77a3511a81c6e56 Mon Sep 17 00:00:00 2001 From: PK-100 <68665655+PK-100@users.noreply.github.com> Date: Tue, 27 Oct 2020 18:29:24 +0530 Subject: [PATCH] Create Start_Of_Linked_List_Cycle.py --- .../Start_Of_Linked_List_Cycle.py | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 leetcode/October LeetCoding Challenge/Start_Of_Linked_List_Cycle.py diff --git a/leetcode/October LeetCoding Challenge/Start_Of_Linked_List_Cycle.py b/leetcode/October LeetCoding Challenge/Start_Of_Linked_List_Cycle.py new file mode 100644 index 0000000..9db8708 --- /dev/null +++ b/leetcode/October LeetCoding Challenge/Start_Of_Linked_List_Cycle.py @@ -0,0 +1,38 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution: + def detectCycle(self, head: ListNode) -> ListNode: + try: + + slow = head.next + fast = head.next.next + + while slow != fast: + slow = slow.next + fast = fast.next.next + + else: + l = 1 + temp = slow.next + while temp!=slow: + temp = temp.next + l+=1 + + p1 = head + p2 = head + for i in range(l): + p2 = p2.next + + while p1!=p2: + p1 = p1.next + p2 = p2.next + + return p1 + + + except: + return None # No Cycle