解析:
分别有两个链表A,B
要找到两者公共的节点,只需要A走完自己的节点,接着走B的Head开始
B走完自己的节点,接着走A的节点,当两者相等时则判断到达第一个公共节点
/**
* 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,B=headB;
while(A!=B){
A=A!=null?A.next:headB;
B=B!=null?B.next:headA;
}
return A;
}
}