160. 相交链表
题目链接:https://leetcode.cn/problems/intersection-of-two-linked-lists/
个人题解记录
1.思路
这题是第二次做了,但还是没有一遍做对,在做的过程中修改了链表的结构,导致连成环,在debug后修正
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode p = headA;
ListNode q = headB;
while(p!=q){
if(p.next==null && q.next==null){
return null;
}
p = p.next == null?headB:p.next;
q = q.next == null?headA:q.next;
}
return p;
}
}