Skip to content

Commit 75d8926

Browse files
committed
add q160
1 parent 68d5bbc commit 75d8926

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- [q25_k个一组翻转链表](/src/链表操作/q25_k个一组翻转链表)
1717
- [q61_旋转链表](/src/链表操作/q61_旋转链表)
1818
- [q138_复制带随机指针的链表](/src/链表操作/q138_复制带随机指针的链表)
19+
- [q160_相交链表](/src/链表操作/q160_相交链表)
1920
- [q206_反转链表](/src/链表操作/q206_反转链表)
2021

2122
### 双指针遍历/滑动窗口

README_EN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- [Question 25 : Reverse Nodes in k-Group](/src/链表操作/q25_k个一组翻转链表)
1717
- [Question 61 : Rotate List](/src/链表操作/q61_旋转链表)
1818
- [Question 138 : Copy List with Random Pointer](/src/链表操作/q138_复制带随机指针的链表)
19+
- [Question 160 : Intersection of Two Linked Lists](/src/链表操作/q160_相交链表)
1920
- [Question 206 : Reverse Linked List](/src/链表操作/q206_反转链表)
2021

2122
### Two Pointers Traversal / Sliding Window
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package 链表操作.q160_相交链表;
2+
3+
public class ListNode {
4+
int val;
5+
ListNode next;
6+
7+
ListNode(int x) {
8+
val = x;
9+
next = null;
10+
}
11+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package 链表操作.q160_相交链表;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
/**
7+
* 哈希存储
8+
*
9+
* 方法二:两个链表相连,快慢指针判断是否有环(省略)
10+
*/
11+
public class Solution {
12+
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
13+
Set<ListNode> visited = new HashSet<>();
14+
ListNode temp = headA;
15+
while (temp != null) {
16+
visited.add(temp);
17+
temp = temp.next;
18+
}
19+
temp = headB;
20+
while (temp != null) {
21+
if (visited.contains(temp)) {
22+
return temp;
23+
}
24+
temp = temp.next;
25+
}
26+
return null;
27+
}
28+
}

0 commit comments

Comments
 (0)