本文共 733 字,大约阅读时间需要 2 分钟。
找到第一个相同的节点
让长的链表的指针先移动差值距离,然后同时移动,判断是否当前节点相同
public class Solution { public ListNode getIntersectionNode(ListNode headA, ListNode headB) { int sizeA = 0; int sizeB = 0; ListNode curA = headA; ListNode curB = headB; while (curA != null) { sizeA++; curA = curA.next; } while (curB != null) { sizeB++; curB = curB.next; } while (sizeA > sizeB) { headA = headA.next; sizeA--; } while (sizeA < sizeB) { headB = headB.next; sizeB--; } while (headA != headB) { headA = headA.next; headB = headB.next; } return headA; }}
转载地址:http://ythld.baihongyu.com/