MCPcopy
hub / github.com/careercup/ctci / findBeginning

Function findBeginning

python/Chapter 2/Question2_6.py:4–25  ·  view source on GitHub ↗
(linkedlist)

Source from the content-addressed store, hash-verified

2
3
4def findBeginning(linkedlist):
5 slow = linkedlist.head
6 fast = linkedlist.head
7
8 # Find meetng point
9 while (fast != None) and (fast.next != None):
10 slow = slow.next
11 fast = fast.next.next
12 if fast == slow:
13 break
14
15 # Check whether it is a circular linked list
16 if fast == None or fast.next == None:
17 return None
18
19 # Move one runner to head. Making them move at same pace, they will meet at the beginning of the loop
20 fast = linkedlist.head
21 while fast != slow:
22 slow = slow.next
23 fast = fast.next
24
25 return fast
26
27
28

Callers 1

Question2_6.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected