160. 相交链表 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) {
        
        if(headA == null || headB == null)
            return null;

        ListNode pA = headA, pB = headB;

        while( pA != pB ){

            pA = pA == null ? headB : pA.next;

            pB = pB == null ? headA : pB.next;

        }

        return pA;

    }
}

上一篇:No.15-VulnHub-Raven: 1-Walkthrough渗透学习


下一篇:链表 160.找出两个链表的交点