Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6b0e2dd

Browse files
author
scuyjzh
committed
modify solutions to problem - "160. Intersection of Two Linked Lists"
1 parent 5c7adb7 commit 6b0e2dd

File tree

2 files changed

+15
-52
lines changed

2 files changed

+15
-52
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@
211211
| 19 | [删除链表的倒数第 N 个节点](https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/) | [Java](./src/com/scuyjzh/leetcode/medium/No_0019_Remove_Nth_Node_From_End_of_List/Solution.java) | 中等 | 链表 双指针 |
212212
| 876 | [链表的中间结点](https://leetcode-cn.com/problems/middle-of-the-linked-list/) | [Java](./src/com/scuyjzh/leetcode/easy/No_0876_Middle_of_the_Linked_List/Solution.java) | 简单 | 链表 双指针 |
213213
| 142 | [环形链表 II](https://leetcode-cn.com/problems/linked-list-cycle-ii/) | [Java](./src/com/scuyjzh/leetcode/medium/No_0142_Linked_List_Cycle_II/Solution.java) | 中等 | 哈希表 链表 双指针 |
214+
| 160 | [相交链表](https://leetcode-cn.com/problems/intersection-of-two-linked-lists/) | [Java](./src/com/scuyjzh/leetcode/easy/No_0160_Intersection_of_Two_Linked_Lists/Solution.java) | 简单 | 哈希表 链表 双指针 |
214215

215216
### Array
216217

‎src/com/scuyjzh/leetcode/easy/No_0160_Intersection_of_Two_Linked_Lists/Solution.java‎

Lines changed: 14 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,74 +2,37 @@
22

33
import com.scuyjzh.leetcode.structure.ListNode;
44

5-
import java.util.*;
6-
75
/**
86
* 160. 相交链表
97
*
108
* 给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表
119
* 相交的起始节点。如果两个链表不存在相交节点,返回 null 。
10+
*
1211
* 题目数据 保证 整个链式结构中不存在环。
13-
* 注意,函数返回结果后,链表必须 保持其原始结构 。
14-
* 进阶:你能否设计一个时间复杂度 O(m + n) 、仅用 O(1) 内存的解决方
15-
* 案?
12+
*
13+
* 进阶:你能否设计一个时间复杂度 O(m + n) 、仅用 O(1) 内存的解决方案?
1614
*/
1715
class Solution {
1816
/**
19-
* 方法一:哈希集合
20-
*/
21-
public ListNode getIntersectionNode1(ListNode headA, ListNode headB) {
22-
/*
23-
* 判断两个链表是否相交,可以使用哈希集合存储链表节点。
24-
*
25-
* 首先遍历链表 headA,并将链表 headA 中的每个节点加入哈希集合中。然后遍历链表 headB,对于遍历到的
26-
* 每个节点,判断该节点是否在哈希集合中:
27-
* • 如果当前节点不在哈希集合中,则继续遍历下一个节点;
28-
* • 如果当前节点在哈希集合中,则后面的节点都在哈希集合中,即从当前节点开始的所有节点都在两个链
29-
* 表的相交部分,因此在链表 headB 中遍历到的第一个在哈希集合中的节点就是两个链表相交的节点,
30-
* 返回该节点。
31-
*
32-
* 如果链表 headB 中的所有节点都不在哈希集合中,则两个链表不相交,返回 null。
33-
*/
34-
Set<ListNode> visited = new HashSet<>();
35-
ListNode temp = headA;
36-
while (temp != null) {
37-
visited.add(temp);
38-
temp = temp.next;
39-
}
40-
temp = headB;
41-
while (temp != null) {
42-
if (visited.contains(temp)) {
43-
return temp;
44-
}
45-
temp = temp.next;
46-
}
47-
return null;
48-
}
49-
50-
/**
51-
* 方法二:双指针
17+
* 方法:双指针
5218
*/
53-
public ListNode getIntersectionNode2(ListNode headA, ListNode headB) {
54-
/*
55-
* pA 走过的路径为 A链 + B链,
56-
* pB 走过的路径为 B链 + A链。
57-
* pA 和 pB 走过的长度相同,都是 A链 和 B链 的长度之和。
58-
*
59-
* 即相当于将两个链表从尾端对齐,如果相交,则会提前在相交点相遇;
60-
* 如果没有相交点,则会在最后相遇。
61-
*
62-
* pA: 4->1->8->4->5->null->5->6->1->[8]->4->5->null
63-
* pB: 5->6->1->8->4->5->null->4->1->[8]->4->5->null
64-
*/
19+
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
20+
// 首先判断链表 headA 和 headB 是否为空,如果其中至少有一个链表为空,则两个链表一定不相交,返回 null
6521
if (headA == null || headB == null) {
6622
return null;
6723
}
24+
// 创建两个指针 pA 和 pB,初始时分别指向两个链表的头节点 headA 和 headB
6825
ListNode pA = headA, pB = headB;
26+
// 然后将两个指针依次遍历两个链表的每个节点,每步操作需要同时更新指针 pA 和 pB
6927
while (pA != pB) {
28+
// 如果指针 pA 不为空,则将指针 pA 移到下一个节点;
29+
// 如果指针 pA 为空,则将指针 pA 移到链表 headB 的头节点
7030
pA = pA == null ? headB : pA.next;
31+
// 如果指针 pB 不为空,则将指针 pB 移到下一个节点;
32+
// 如果指针 pB 为空,则将指针 pB 移到链表 headA 的头节点
7133
pB = pB == null ? headA : pB.next;
7234
}
35+
// 当指针 pA 和 pB 指向同一个节点或者都为空时,返回它们指向的节点或者 null
7336
return pA;
7437
}
7538

@@ -87,7 +50,6 @@ public static void main(String[] args) {
8750
headB.next.next = new ListNode(1);
8851
headB.next.next.next = intersectionNode;
8952

90-
System.out.println(ListNode.toString(new Solution().getIntersectionNode1(headA, headB)));
91-
System.out.println(ListNode.toString(new Solution().getIntersectionNode2(headA, headB)));
53+
System.out.println(new Solution().getIntersectionNode(headA, headB).val);
9254
}
9355
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /