Intersection point of two Linked Lists | Set 3 - GeeksforGeeks

05 Jul, 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 → 30Output: 15Explanation:
From the above image, the intersection point of the two linked lists is 15.
Input: L1: 1 → 2 → 3, L2: 4 → 5 → 1 → 2 → 3Output: 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++
Java
Python3
C#
Javascript
// C++ program for the above approach #include <bits/stdc++.h>using namespace std; // Structure of a node// of a Linked Listclass Node {public:    int data;    Node* next;     // Constructor    Node(int x)    {        data = x;        next = NULL;    }}; // Function to find the intersection// point of the two Linked ListsNode* 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 listsvoid 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 Codeint main(){    Node* head1;    Node* head2;    formLinkList(head1, head2);     cout << abs(intersectingNode(head1,                                 head2)                    ->data);     return 0;}
// Java program for the above approachimport java.io.*; class GFG{     static Node head1 = null;static Node head2 = null; // Structure of a node// of a Linked Liststatic class Node{    int data;    Node next;     // Constructor    Node(int x)    {        data = x;        next = null;    }} // Function to find the intersection// point of the two Linked Listsstatic Node intersectingNode(Node headA, Node headB){         // Traverse the first linked list    // and multiply all values by -1    Node a = headA;     while (a != null)    {                 // 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 != null)    {                 // Intersection point        if (b.data < 0)            break;         // Update b        b = b.next;    }    return b;} // Function to create linked listsstatic void formLinkList(){         // 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 Codepublic static void main(String[] args){    formLinkList();     System.out.println(Math.abs(        intersectingNode(head1, head2).data));}} // This code is contributed by Dharanendra L V.
# Python3 program for the above approach # Structure of a node# of a Linked Listclass Node:         def __init__(self, d):                 self.data = d        self.next = None # Function to find the intersection# point of the two Linked Listsdef intersectingNode(headA, headB):     # Traverse the first linked list    # and multiply all values by -1    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    b = headB     while (b):         # Intersection point        if (b.data < 0):            break         # Update b        b = b.next     return b # Function to create linked listsdef formLinkList(head1, head2):         # Linked List L1    head1 = Node(3)    head1.next = Node(6)    head1.next.next = Node(9)    head1.next.next.next = Node(15)    head1.next.next.next.next = Node(30)     # Linked List L2    head2 = Node(10)    head2.next = head1.next.next.next     return head1, head2 # Driver Codeif __name__ == '__main__':         head1, head2 = formLinkList(None, None)     print(abs(intersectingNode(head1, head2).data)) # This code is contributed by mohit kumar 29
// C# program for the above approach using System; public class Node{    public int data;    public Node next;      // Constructor    public Node(int x)    {        data = x;        next = null;    }} public class GFG{         static Node head1 = null;static Node head2 = null;// Function to find the intersection// point of the two Linked Listsstatic Node intersectingNode(Node headA, Node headB){          // Traverse the first linked list    // and multiply all values by -1    Node a = headA;      while (a != null)    {                  // 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 != null)    {                  // Intersection point        if (b.data < 0)            break;          // Update b        b = b.next;    }    return b;}  // Function to create linked listsstatic void formLinkList(){          // 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         static public void Main ()    {                 formLinkList();      Console.WriteLine(Math.Abs(        intersectingNode(head1, head2).data));             }} // This code is contributed by unknown2108.
<script> // JavaScript program for the above approach let head1 = null;let head2 = null; // Structure of a node// of a Linked Listclass Node{    constructor(x)    {        this.data=x;        this.next=null;    }} // Function to find the intersection// point of the two Linked Listsfunction intersectingNode(headA,headB){    // Traverse the first linked list    // and multiply all values by -1    let a = headA;      while (a != null)    {                  // 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    let b = headB;      while (b != null)    {                  // Intersection point        if (b.data < 0)            break;          // Update b        b = b.next;    }    return b;} // Function to create linked listsfunction formLinkList(){    // 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 CodeformLinkList(); document.write(Math.abs(intersectingNode(head1, head2).data)); // This code is contributed by patel2127 </script>
15
 
Time Complexity: O(N + M)Auxiliary Space: O(1)
mohit kumar 29
dharanendralv23
patel2127
unknown2108
Linked Lists
Data Structures
Greedy
Linked List
Searching
Data Structures
Linked List
Searching
Greedy
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Introduction to Tree Data Structure
Program to implement Singly Linked List in C++ using class
TCS NQT Coding Sheet
Hash Functions and list/types of Hash functions
Building an undirected graph and finding shortest path using Dictionaries in Python
Dijkstra's shortest path algorithm | Greedy Algo-7
Prim’s Minimum Spanning Tree (MST) | Greedy Algo-5
Program for array rotation
Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
Huffman Coding | Greedy Algo-3