Note:

If two lists do not intersect, null is returned.

After the result is returned, both lists must remain in their original structure.

It can be assumed that there are no loops in the entire list structure.

The program meets the O(n) time complexity as far as possible and uses only O(1) memory.

160.Leetcode-cn.com/problems/in…

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) {
        
        ListNode h1 = headA, h2 = headB;
        while(h1 ! = h2) { h1 = h1 == null ? headB : h1.next; h2 = h2 == null ? headA : h2.next; }returnh1; }}Copy the code