经典链表判断相交,记住就完事了。
/**
* 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) {
ListNode a = headA;
ListNode b = headB;
int cnt = 0;
while (cnt <= 2) {
if (a == b) return a;
if (a == null) {
a = headB;
cnt++;
} else {
a = a.next;
}
if (b == null) {
b = headA;
cnt++;
} else {
b = b.next;
}
}
return null;
}
}