160. Intersection of Two Linked Lists

len(the shorter one) + len(the longer one) == len(the longer one) + len(the shorter one), thus if they have intersection, it will be the end.

/**
 * 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) {
        ListNode pA = headA;
        ListNode pB = headB;
        // if they have not intersection pA == null and pB == null,
        // otherwise pA == intersection, pB == intersection 
        while (pA != pB) {
            pA = (pA == null) ? headB : pA.next;
            pB = (pB == null) ? headA : pB.next;
        }
        return pA;
    }
}