-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathproblem_20.py
31 lines (29 loc) · 1.04 KB
/
problem_20.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
def coding_problem_20(list_a, list_b):
"""
Given two singly linked lists that intersect at some point, find the intersecting node.
Do this in O(M + N) time (where M and N are the lengths of the lists) and constant space.
For example, given A = 3 -> 7 -> 8 -> 10 -> 1 and B = 99 -> 1 -> 8 -> 10, return the node with value 8.
Example:
>>> class LinkedListNode(object):
...
... def __init__(self, value, child=None):
... self.value = value
... self.next = child
...
... def add(self, value):
... return LinkedListNode(value, self)
...
... @classmethod
... def len(cls, node):
... count = 0
... while node:
... node = node.next
... count += 1
... return count
>>> common_tail = LinkedListNode(1).add(10).add(8)
>>> list_a = LinkedListNode(7, common_tail).add(3)
>>> list_b = LinkedListNode(1, common_tail).add(99).add(14)
>>> coding_problem_20(list_a, list_b)
8
"""
pass