The Wayback Machine - https://web.archive.org/web/20210326182727/https://www.geeksforgeeks.org/intersection-point-of-two-linked-lists-set-3/
Skip to content
Related Articles

Related Articles

Intersection point of two Linked Lists | Set 3
  • Last Updated : 25 Mar, 2021

Given two linked lists of size N and M consisting of positive value nodes, having a common intersection point, the task is to find the intersection point of the two linked lists where they merge.

Examples:

Input: L1: 3 → 6 → 9 → 15 → 30, L2: 10 → 15 → 30
Output: 15
Explanation:

From the above image, the intersection point of the two linked lists is 15.



Input: L1: 1 → 2 → 3, L2: 4 → 5 → 1 → 2 → 3
Output: 1

Approach: The idea is to traverse the first linked list and multiply the value of each node by -1 thus making them negative. Then, traverse the second linked list and print the value of the first node having a negative value. Follow the steps below to solve the problem:

  • Traverse the first linked list L1 and multiply the value of each node by -1.
  • Now, traverse the second linked list L2 and if there exists any node with negative values then print the absolute value of the node’s value as the resultant intersection of the linked list and break out of the loop.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Structure of a node
// of a Linked List
class Node {
public:
    int data;
    Node* next;
  
    // Constructor
    Node(int x)
    {
        data = x;
        next = NULL;
    }
};
  
// Function to find the intersection
// point of the two Linked Lists
Node* intersectingNode(Node* headA,
                       Node* headB)
{
  
    // Traverse the first linked list
    // and multiply all values by -1
    Node* a = headA;
  
    while (a) {
  
        // Update a -> data
        a->data *= -1;
  
        // Update a
        a = a->next;
    }
  
    // Traverse the second Linked List
    // and find the value of the first
    // node having negative value
    Node* b = headB;
  
    while (b) {
  
        // Intersection point
        if (b->data < 0)
            break;
  
        // Update b
        b = b->next;
    }
  
    return b;
}
  
// Function to create linked lists
void formLinkList(Node*& head1,
                  Node*& head2)
{
    // Linked List L1
    head1 = new Node(3);
    head1->next = new Node(6);
    head1->next->next = new Node(9);
    head1->next->next->next = new Node(15);
    head1->next->next->next->next = new Node(30);
  
    // Linked List L2
    head2 = new Node(10);
    head2->next = head1->next->next->next;
  
    return;
}
  
// Driver Code
int main()
{
    Node* head1;
    Node* head2;
    formLinkList(head1, head2);
  
    cout << abs(intersectingNode(head1,
                                 head2)
                    ->data);
  
    return 0;
}
Output:
15

Time Complexity: O(N + M)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Recommended Articles
Page :