forked from piyush01123/Daily-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sol.py
41 lines (35 loc) · 949 Bytes
/
sol.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
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def getIntersectionNode(self, headA, headB):
"""
:type head1, head1: ListNode
:rtype: ListNode
"""
# Solution 1
# A = set()
# while headA:
# A.add(headA)
# headA = headA.next
# while headB:
# # print(A)
# if headB in A:
# return headB
# A.add(headB)
# headB = headB.next
# Solution 2
currA = headA
currB = headB
while currA != currB:
if currA is None:
currA = headB
else:
currA = currA.next
if currB is None:
currB = headA
else:
currB = currB.next
return currA