Skip to content

Commit 5ab9a5b

Browse files
committed
feat: add python and java solutions to lcof problem
添加《剑指 Offer》题解:面试题52. 两个链表的第一个公共节点
1 parent 3f90a76 commit 5ab9a5b

File tree

3 files changed

+214
-0
lines changed

3 files changed

+214
-0
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# [面试题52. 两个链表的第一个公共节点](https://leetcode-cn.com/problems/liang-ge-lian-biao-de-di-yi-ge-gong-gong-jie-dian-lcof/)
2+
3+
## 题目描述
4+
输入两个链表,找出它们的第一个公共节点。
5+
6+
如下面的两个链表:
7+
8+
![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/14/160_statement.png)
9+
10+
在节点 c1 开始相交。
11+
12+
**示例 1:**
13+
14+
![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/14/160_example_1.png)
15+
16+
```
17+
输入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
18+
输出:Reference of the node with value = 8
19+
输入解释:相交节点的值为 8 (注意,如果两个列表相交则不能为 0)。从各自的表头开始算起,链表 A 为 [4,1,8,4,5],链表 B 为 [5,0,1,8,4,5]。在 A 中,相交节点前有 2 个节点;在 B 中,相交节点前有 3 个节点。
20+
```
21+
22+
**示例 2:**
23+
24+
![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/14/160_example_2.png)
25+
26+
```
27+
输入:intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
28+
输出:Reference of the node with value = 2
29+
输入解释:相交节点的值为 2 (注意,如果两个列表相交则不能为 0)。从各自的表头开始算起,链表 A 为 [0,9,1,2,4],链表 B 为 [3,2,4]。在 A 中,相交节点前有 3 个节点;在 B 中,相交节点前有 1 个节点。
30+
```
31+
32+
**示例 3:**
33+
34+
![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/14/160_example_3.png)
35+
36+
```
37+
输入:intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
38+
输出:null
39+
输入解释:从各自的表头开始算起,链表 A 为 [2,6,4],链表 B 为 [1,5]。由于这两个链表不相交,所以 intersectVal 必须为 0,而 skipA 和 skipB 可以是任意值。
40+
解释:这两个链表不相交,因此返回 null。
41+
```
42+
43+
**注意:**
44+
45+
- 如果两个链表没有交点,返回 `null`
46+
- 在返回结果后,两个链表仍须保持原有的结构。
47+
- 可假定整个链表结构中没有循环。
48+
- 程序尽量满足 O(n) 时间复杂度,且仅用 O(1) 内存。
49+
50+
## 解法
51+
### Python3
52+
```python
53+
# Definition for singly-linked list.
54+
# class ListNode:
55+
# def __init__(self, x):
56+
# self.val = x
57+
# self.next = None
58+
59+
class Solution:
60+
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
61+
if headA is None or headB is None:
62+
return None
63+
len1 = len2 = 0
64+
p, q = headA, headB
65+
while p:
66+
p = p.next
67+
len1 += 1
68+
while q:
69+
q = q.next
70+
len2 += 1
71+
p, q = headA, headB
72+
if len1 > len2:
73+
p, q = q, p
74+
for _ in range(abs(len1 - len2)):
75+
q = q.next
76+
while p and q:
77+
if p == q:
78+
return p
79+
p = p.next
80+
q = q.next
81+
82+
```
83+
84+
### Java
85+
```java
86+
/**
87+
* Definition for singly-linked list.
88+
* public class ListNode {
89+
* int val;
90+
* ListNode next;
91+
* ListNode(int x) {
92+
* val = x;
93+
* next = null;
94+
* }
95+
* }
96+
*/
97+
public class Solution {
98+
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
99+
if (headA == null || headB == null) {
100+
return null;
101+
}
102+
ListNode p = headA, q = headB;
103+
int len1 = len(p), len2 = len(q);
104+
if (len1 > len2) {
105+
ListNode t = headA;
106+
headA = headB;
107+
headB = t;
108+
}
109+
p = headA;
110+
q = headB;
111+
for (int i = 0; i < Math.abs(len1 - len2); ++i) {
112+
q = q.next;
113+
}
114+
while (p != null && q != null) {
115+
if (p == q) {
116+
return p;
117+
}
118+
p = p.next;
119+
q = q.next;
120+
}
121+
return null;
122+
}
123+
124+
private int len(ListNode node) {
125+
int len = 0;
126+
while (node != null) {
127+
node = node.next;
128+
++len;
129+
}
130+
return len;
131+
}
132+
}
133+
```
134+
135+
### ...
136+
```
137+
138+
```
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) {
7+
* val = x;
8+
* next = null;
9+
* }
10+
* }
11+
*/
12+
public class Solution {
13+
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
14+
if (headA == null || headB == null) {
15+
return null;
16+
}
17+
ListNode p = headA, q = headB;
18+
int len1 = len(p), len2 = len(q);
19+
if (len1 > len2) {
20+
ListNode t = headA;
21+
headA = headB;
22+
headB = t;
23+
}
24+
p = headA;
25+
q = headB;
26+
for (int i = 0; i < Math.abs(len1 - len2); ++i) {
27+
q = q.next;
28+
}
29+
while (p != null && q != null) {
30+
if (p == q) {
31+
return p;
32+
}
33+
p = p.next;
34+
q = q.next;
35+
}
36+
return null;
37+
}
38+
39+
private int len(ListNode node) {
40+
int len = 0;
41+
while (node != null) {
42+
node = node.next;
43+
++len;
44+
}
45+
return len;
46+
}
47+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
class Solution:
8+
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
9+
if headA is None or headB is None:
10+
return None
11+
len1 = len2 = 0
12+
p, q = headA, headB
13+
while p:
14+
p = p.next
15+
len1 += 1
16+
while q:
17+
q = q.next
18+
len2 += 1
19+
p, q = headA, headB
20+
if len1 > len2:
21+
p, q = q, p
22+
for _ in range(abs(len1 - len2)):
23+
q = q.next
24+
while p and q:
25+
if p == q:
26+
return p
27+
p = p.next
28+
q = q.next
29+

0 commit comments

Comments
 (0)