160 相交链表

  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. * int val;
  5. * ListNode next;
  6. * ListNode(int x) {
  7. * val = x;
  8. * next = null;
  9. * }
  10. * }
  11. */
  12. public class Solution {
  13. public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
  14. ListNode p = headA, q = headB;
  15. while (p != q) {
  16. p = (p != null) ? p = p.next : headB;
  17. q = (q != null) ? q = q.next : headA;
  18. }
  19. return p;
  20. }
  21. }