剑指 Offer 52. 两个链表的第一个公共节点
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode first = headA;
ListNode second= headB;
while(first!=second){
first=first!=null?first.next:headB;
second=second!=null?second.next:headA;
}
return first;
}
两个链表长度分别为L1+C、L2+C, C为公共部分的长度,按照楼主的做法: 第一个人走了L1+C步后,回到第二个人起点走L2步;第2个人 走了L2+C步后,回到第一个人起点走L1步。 当两个人走的步数都为L1+L2+C时就两个家伙就相爱了.