Lc160_相交鏈表

160. 相交鏈表
 1public class GetIntersectionNode {
2
3    private static class ListNode {
4        int val;
5        ListNode next;
6
7        ListNode(int x) {
8            val = x;
9            next = null;
10        }
11    }
12
13    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
14        if (headA == null || headB == null) {
15            return null;
16        }
17
18        ListNode pA = headA, pB = headB;
19        while (pA != pB) {
20            pA = pA == null ? headB : pA.next;
21            pB = pB == null ? headA : pB.next;
22        }
23
24        return pA;
25    }
26
27
28}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章