160.相交链表

点击查看代码
/**
 * 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) {
      
        if(headA == null || headB == null) return null;

        ListNode pA = headA;
        ListNode pB = headB;
        while(pA != pB) {
            pA = (pA == null) ? headB : pA.next;
            pB = (pB == null) ? headA : pB.next; 
        }

        return pA;
    }
}
posted @ 2026-02-05 16:11  AnoSky  阅读(5)  评论(0)    收藏  举报