1
+ # Given the heads of two singly linked-lists headA and headB, return the node
2
+ # at which the two lists intersect. If the two linked lists have no intersection
3
+ # at all, return null.
4
+
5
+ # For example, the following two linked lists begin to intersect at node c1:
6
+
7
+
8
+ # The test cases are generated such that there are no cycles anywhere in the
9
+ # entire linked structure.
10
+
11
+ # Note that the linked lists must retain their original structure after the
12
+ # function returns.
13
+
14
+ # Custom Judge:
15
+ # The inputs to the judge are given as follows (your program is not given these inputs):
16
+
17
+ # intersectVal - The value of the node where the intersection occurs. This is 0 if
18
+ # there is no intersected node.
19
+
20
+ # listA - The first linked list.
21
+ # listB - The second linked list.
22
+ # skipA - The number of nodes to skip ahead in listA (starting from the head) to get
23
+ # to the intersected node.
24
+ # skipB - The number of nodes to skip ahead in listB (starting from the head) to get
25
+ # to the intersected node.
26
+ # The judge will then create the linked structure based on these inputs and pass the
27
+ # two heads, headA and headB to your program. If you correctly return the intersected
28
+ # node, then your solution will be accepted.
29
+
30
+
31
+ # Example 1:
32
+ # Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3
33
+ # Output: Intersected at '8'
34
+ # Explanation: The intersected node's value is 8 (note that this must not be 0 if the two
35
+ # lists intersect).
36
+ # From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,6,1,8,4,5].
37
+ # There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B.
38
+ # - Note that the intersected node's value is not 1 because the nodes with value 1 in A and B
39
+ # (2nd node in A and 3rd node in B) are different node references. In other words, they point
40
+ # to two different locations in memory, while the nodes with value 8 in A and B
41
+ # (3rd node in A and 4th node in B) point to the same location in memory.
42
+
43
+ # Example 2:
44
+ # Input: intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
45
+ # Output: Intersected at '2'
46
+ # Explanation: The intersected node's value is 2 (note that this must not be 0 if the
47
+ # two lists intersect).
48
+ # From the head of A, it reads as [1,9,1,2,4]. From the head of B, it reads as [3,2,4].
49
+ # There are 3 nodes before the intersected node in A; There are 1 node before the intersected
50
+ # node in B.
51
+
52
+ # Example 3:
53
+ # Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
54
+ # Output: No intersection
55
+ # Explanation: From the head of A, it reads as [2,6,4]. From the head of B, it reads as [1,5].
56
+ # Since the two lists do not intersect, intersectVal must be 0, while skipA and skipB can be
57
+ # arbitrary values.
58
+ # Explanation: The two lists do not intersect, so return null.
59
+
60
+
61
+ # Constraints:
62
+
63
+ # The number of nodes of listA is in the m.
64
+ # The number of nodes of listB is in the n.
65
+ # 1 <= m, n <= 3 * 104
66
+ # 1 <= Node.val <= 105
67
+ # 0 <= skipA <= m
68
+ # 0 <= skipB <= n
69
+ # intersectVal is 0 if listA and listB do not intersect.
70
+ # intersectVal == listA[skipA] == listB[skipB] if listA and listB intersect.
71
+
72
+
73
+
74
+ from typing import Optional
75
+
76
+ # Definition for singly-linked list.
77
+ class ListNode :
78
+ def __init__ (self , x ):
79
+ self .val = x
80
+ self .next = None
81
+
82
+ class Solution :
83
+ def getIntersectionNode (self , headA : ListNode , headB : ListNode ) -> Optional [ListNode ]:
84
+ a = headA
85
+ b = headB
86
+
87
+ while a != b :
88
+ a = a .next if a else headB
89
+ b = b .next if b else headA
90
+ return a
0 commit comments