-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathSolution0160.java
More file actions
53 lines (47 loc) · 1.34 KB
/
Copy pathSolution0160.java
File metadata and controls
53 lines (47 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// 160. 相交链表
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
/*
分别遍历两个链表,使用列表存放第一个链表的节点,遍历第二个链表的节点时,如果列表存在该节点,则为相交节点
*/
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
List<ListNode> list = new ArrayList<>();
while (headA != null) {
list.add(headA);
headA = headA.next;
}
while (headB != null) {
if (list.contains(headB)) {
return headB;
}
headB = headB.next;
}
return null;
}
}
/*
双指针:使用两个指针都遍历两个链表,由于走的长度一致,最终会在某点相遇,该点就是相交起始结点,如果没相遇则都走到空指针
*/
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if (headA == null || headB == null) {
return null;
}
ListNode A = headA, B = headB;
while (A != B) {
A = A == null ? headB : A.next;
B = B == null ? headA : B.next;
}
return A;
}
}