underwindfall / Algorithme

练习总结算法的地方
https://qifanyang.com/resume
1 stars 0 forks source link

LCOF23 #362

Closed underwindfall closed 2 years ago

underwindfall commented 2 years ago
//time O(m + n)
    //space O(1)
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode node1 = headA, node2 = headB;

        while (node1 != node2) {
            if (node1.next != null) {
                node1 = node1.next;
            } else {
                node1 = headB;
            }
            if (node2.next != null) {
                node2 = node2.next;
            } else {
                node2 = headA;
            }
        }
        return node1;

    }