# Simulation volume Leetcode [sword finger Offer]_ 52. The first common node of two linked lists

### Offer_day12_52. The first common node of two linked lists

Enter two linked lists and find their first common node.

As shown in the following two linked lists:

The intersection begins at node c1.

Example 1:

Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
Output: Reference of the node with value = 8
Input explanation: the value of intersection node is 8 (note that if two lists intersect, it cannot be 0). Starting from their respective headers, linked list A is [4,1,8,4,5], and linked list B is [5,0,1,8,4,5]. In A, there are 2 nodes before the intersection node; In B, there are 3 nodes before the intersection node.

Example   2:

Input: intersectVal  = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
Output: Reference of the node with value = 2
Input explanation: the value of intersection node is 2 (note that if two lists intersect, it cannot be 0). Starting from the respective header, linked list A is [0,9,1,2,4], and linked list B is [3,2,4]. In A, there are 3 nodes before the intersection node; In B, there is 1 node before the intersection node.

Example   3:

Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
Output: null
Input explanation: starting from the respective header, linked list A is [2,6,4], and linked list B is [1,5]. Because the two linked lists do not intersect, intersectVal must be 0, and skipA and skipB can be any value.
Explanation: the two linked lists do not intersect, so null is returned.

be careful:

If two linked lists have no intersection, null is returned
After returning the results, the two linked lists must still maintain their original structure.
It can be assumed that there is no loop in the whole linked list structure.
The program shall meet the O(n) time complexity as much as possible, and only use O(1) memory.
This question is the same as the main station question 160: https://leetcode-cn.com/problems/intersection-of-two-linked-lists/

Source: LeetCode

code:

```import time
from typing import List

class ListNode:
def __init__(self, x):
self.val = x
self.next = None

class Solution:
def __init__(self):
pass

set_a = set()

def test(data_test):
s = Solution()
return s.getResult(*data_test)

def test_obj(data_test):
result = [None]
obj = Solution(*data_test)
for fun, data in zip(data_test[1::], data_test[1::]):
if data:
res = obj.__getattribute__(fun)(*data)
else:
res = obj.__getattribute__(fun)()
result.append(res)
return result

if __name__ == '__main__':
datas = [
[],
]
for data_test in datas:
t0 = time.time()
print('-' * 50)
print('input:', data_test)
print('output:', test(data_test))
print(f'use time:{time.time() - t0}s')
```

remarks:
GitHub: https://github.com/monijuan/leetcode_python

Can add QQ group communication: 1092754609

leetcode_python.utils is described in the summary page
Brush the questions first, and then use the blog generated by script. If there is any error, please leave a message. I will modify it when I see it! thank you!

Posted on Fri, 12 Nov 2021 03:06:49 -0500 by ReKoNiZe