-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntersectionOfTwoLinkedLists.py
42 lines (41 loc) · 1.21 KB
/
IntersectionOfTwoLinkedLists.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
class Solution:
# @param two ListNodes
# @return the intersected ListNode
def getIntersectionNode(self, headA, headB):
curA,curB = headA,headB
flagA, flagB = True, True
while curA != curB:
if (not curA) and flagA:
curA = headB
flagA = False
else:
curA = curA.next
if (not curB) and flagB:
curB = headA
flagB = False
else:
curB = curB.next
return curA
class Solution:
# @param two ListNodes
# @return the intersected ListNode
def getIntersectionNode(self, headA, headB):
curA,curB = headA,headB
lenA,lenB = 0,0
while curA is not None:
lenA += 1
curA = curA.next
while curB is not None:
lenB += 1
curB = curB.next
curA,curB = headA,headB
if lenA > lenB:
for i in range(lenA-lenB):
curA = curA.next
elif lenB > lenA:
for i in range(lenB-lenA):
curB = curB.next
while curB != curA:
curB = curB.next
curA = curA.next
return curA