-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathIntersection_in_linkedlist.py
55 lines (37 loc) · 1.48 KB
/
Intersection_in_linkedlist.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
def getIntersectionNode(headA,headB):
cnt1=0
cnt2=0
temp1=headA
while temp1:
temp1=temp1.next
cnt1+=1
temp2=headB
while temp2:
temp2=temp2.next
cnt2+=1
if cnt2>cnt1:
l=cnt2-cnt1
temp1=headB
temp2=headA
for i in range(l):
temp1=temp1.next
while temp1 and temp2:
if temp1==temp2:
return temp1
temp1=temp1.next
temp2=temp2.next
else:
return None
else:
l=cnt1-cnt2
temp1=headA
temp2=headB
for i in range(l):
temp1=temp1.next
while temp1 and temp2:
if temp1==temp2:
return temp1
temp1=temp1.next
temp2=temp2.next
else:
return None