File tree Expand file tree Collapse file tree 4 files changed +41
-0
lines changed Expand file tree Collapse file tree 4 files changed +41
-0
lines changed Original file line number Diff line number Diff line change 16
16
- [ q25_k个一组翻转链表] ( /src/链表操作/q25_k个一组翻转链表 )
17
17
- [ q61_旋转链表] ( /src/链表操作/q61_旋转链表 )
18
18
- [ q138_复制带随机指针的链表] ( /src/链表操作/q138_复制带随机指针的链表 )
19
+ - [ q160_相交链表] ( /src/链表操作/q160_相交链表 )
19
20
- [ q206_反转链表] ( /src/链表操作/q206_反转链表 )
20
21
21
22
### 双指针遍历/滑动窗口
Original file line number Diff line number Diff line change 16
16
- [ Question 25 : Reverse Nodes in k-Group] ( /src/链表操作/q25_k个一组翻转链表 )
17
17
- [ Question 61 : Rotate List] ( /src/链表操作/q61_旋转链表 )
18
18
- [ Question 138 : Copy List with Random Pointer] ( /src/链表操作/q138_复制带随机指针的链表 )
19
+ - [ Question 160 : Intersection of Two Linked Lists] ( /src/链表操作/q160_相交链表 )
19
20
- [ Question 206 : Reverse Linked List] ( /src/链表操作/q206_反转链表 )
20
21
21
22
### Two Pointers Traversal / Sliding Window
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments