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}