Intersection of linked list

    0

    0

    Mansimar Anand

    Find Intersection of two linked list

    /**
     * Definition for singly-linked list.
     * Definition for singly-linked list.
     * struct ListNode {
     * struct ListNode {
     *     int val;
     *     int val;
     *     ListNode *next;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     * };
     */
     */
    class Solution {
    public:
        ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
            ListNode* p1 = headA;
            ListNode* p2 = headB;
            while (p1 != p2) {
                p1 = p1 ? p1->next : headA;
                p2 = p2 ? p2->next : headB;
            }
            return p1;
        }
    };
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Explore
    • Cookbooks
    • Playground
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.