코테준비 - Intersection of Two Linked Lists

정상화·2023년 2월 26일

LeetCode

목록 보기
151/222

Intersection of Two Linked Lists

class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        auto nodeA = headA;
        auto nodeB = headB;

        while (nodeA != nullptr && nodeB != nullptr) {
            nodeA = nodeA->next;
            nodeB = nodeB->next;
        }

        int diff = 0;
        bool aIsShorter = false;
        while (nodeB != nullptr) {
            aIsShorter = true;
            nodeB = nodeB->next;
            diff++;
        }
        while (nodeA != nullptr) {
            nodeA = nodeA->next;
            diff++;
        }
        nodeA = headA;
        nodeB = headB;
        if (aIsShorter) {
            while (diff--) {
                nodeB = nodeB->next;
            }
        } else {
            while (diff--) {
                nodeA = nodeA->next;
            }
        }

        while (nodeA != nullptr && nodeB != nullptr && nodeA != nodeB) {
            nodeA = nodeA->next;
            nodeB = nodeB->next;
        }

        if (nodeA == nullptr || nodeB == nullptr) {
            return nullptr;
        }
        return nodeA;
    }
};
profile
백엔드 희망

0개의 댓글