Title Description
To a linked list, wherein if the ring comprises, find the entry ring node list, otherwise, outputs null.
Thinking
If the slow walk of length L 2L gone so fast
Assuming that the length from the start point to the entry s, slow down the ring in a length d
Then l = s + d
Suppose the length of the ring is not gone slow m, fast walking is a length of n * (m + d) + d + s = 2L
Obtained into n * (m + d) + d + s = 2 (s + d) => s = m + (n-1) (m + d) m + d is a circle around the ring so it met s = m after so slow and head go together, the meeting point is the ingress point
1 class Solution: 2 def EntryNodeOfLoop(self, pHead): 3 # write code here 4 if pHead == None: 5 return None 6 fastPointer = pHead 7 slowPointer = pHead 8 while fastPointer and fastPointer.next: 9 slowPointer = slowPointer.next 10 fastPointer = fastPointer.next.next 11 if fastPointer == slowPointer: 12 break 13 if fastPointer == None or fastPointer.next == None: 14 return None 15 fastPointer = pHead 16 while fastPointer!=slowPointer: 17 fastPointer=fastPointer.next 18 slowPointer=slowPointer.next 19 return fastPointer
2019-12-31 22:17:13