本文共 804 字,大约阅读时间需要 2 分钟。
思路:
先求出链表长度。然后对齐指针。然后两个指针一起往后走。这样遇到的第一个一样的节点就是相交的起始点。自己画画图就能明白。。/** * 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 temp; //求长度 int linkALength=0; temp=headA; while(temp!=null) { temp=temp.next; linkALength++; } int linkBLength=0; temp=headB; while(temp!=null) { temp=temp.next; linkBLength++; } //对齐 ListNode linkA=headA; ListNode linkB=headB; if(linkALength>linkBLength) { for(int i=0;i
转载地址:http://xduvb.baihongyu.com/