-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path160.intersection_of_two_linked_lists.py
More file actions
40 lines (33 loc) · 1.02 KB
/
Copy path160.intersection_of_two_linked_lists.py
File metadata and controls
40 lines (33 loc) · 1.02 KB
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
# https://leetcode.com/problems/intersection-of-two-linked-lists
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
"""
if not headA or not headB:
return None
a_iter = self.iterateOverTwoLists(headA, headB)
b_iter = self.iterateOverTwoLists(headB, headA)
while True:
try:
a = a_iter.next()
b = b_iter.next()
except StopIteration:
return None
if a is b:
return a
@staticmethod
def iterateOverTwoLists(headA, headB):
cur_head = headA
while cur_head:
yield cur_head
cur_head = cur_head.next
cur_head = headB
while cur_head:
yield cur_head
cur_head = cur_head.next