题目描述
解题思路
A=A+B,B=B+A
这样处理为了保证链表能够对齐(之后遍历就可以找到相交点)
运行结果
java代码
/** * 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 p=headA; ListNode q=headB; boolean flagA=true,flagB=true; while(p != null && q != null){ if(p==q) return p; p=p.next; q=q.next; if(p == null && flagA){ p=headB; flagA=false; } if(q == null && flagB){ q=headA; flagB=false; } } return null; } }