LeetCode 160. Intersection of Two Linked Lists【Java】

Title description

intersection-of-two-linked-lists

AC code

/**
 * 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) {
        if(headA==null||headB==null)
            return null;
        ListNode hA=headA,hB=headB;
        while(hA!=hB){
            hA=hA==null?headA:hA.next;
            hB=hB==null?headB:hB.next;
        }
        return hA;
    }
}
Published 201 original articles · Like9 · Visitors 10,000+

Guess you like

Origin blog.csdn.net/weixin_40992982/article/details/105473012