-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntersectionOfTwoLinkedList.py
More file actions
40 lines (39 loc) · 973 Bytes
/
IntersectionOfTwoLinkedList.py
File metadata and controls
40 lines (39 loc) · 973 Bytes
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
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# @param two ListNodes
# @return the intersected ListNode
def getIntersectionNode(self, headA, headB):
lenA = 0
lenB = 0
p = headA
while p is not None:
lenA = lenA + 1
p = p.next
p = headB
while p is not None:
lenB = lenB + 1
p = p.next
if lenA > lenB:
less = headB
more = headA
long = lenA
short = lenB
else:
less = headA
more = headB
long = lenB
short = lenA
p = more
for i in range(long - short):
p = p.next
p1 = less
while p is not None:
if p1 is p:
return p
p = p.next
p1 = p1.next
return None