Intersection point of two Linked List by marking visited nodes
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++
#include <bits/stdc++.h>
using namespace std;
class Node {
public :
int data;
Node* next;
Node( int x)
{
data = x;
next = NULL;
}
};
Node* intersectingNode(Node* headA,
Node* headB)
{
Node* a = headA;
while (a) {
a->data *= -1;
a = a->next;
}
Node* b = headB;
while (b) {
if (b->data < 0)
break ;
b = b->next;
}
return b;
}
void formLinkList(Node*& head1,
Node*& head2)
{
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);
head2 = new Node(10);
head2->next = head1->next->next->next;
return ;
}
int main()
{
Node* head1;
Node* head2;
formLinkList(head1, head2);
cout << abs (intersectingNode(head1,
head2)
->data);
return 0;
}
|
Java
import java.io.*;
class GFG{
static Node head1 = null ;
static Node head2 = null ;
static class Node
{
int data;
Node next;
Node( int x)
{
data = x;
next = null ;
}
}
static Node intersectingNode(Node headA, Node headB)
{
Node a = headA;
while (a != null )
{
a.data *= - 1 ;
a = a.next;
}
Node b = headB;
while (b != null )
{
if (b.data < 0 )
break ;
b = b.next;
}
return b;
}
static void formLinkList()
{
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 );
head2 = new Node( 10 );
head2.next = head1.next.next.next;
return ;
}
public static void main(String[] args)
{
formLinkList();
System.out.println(Math.abs(
intersectingNode(head1, head2).data));
}
}
|
Python3
class Node:
def __init__( self , d):
self .data = d
self . next = None
def intersectingNode(headA, headB):
a = headA
while (a):
a.data * = - 1
a = a. next
b = headB
while (b):
if (b.data < 0 ):
break
b = b. next
return b
def formLinkList(head1, head2):
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 )
head2 = Node( 10 )
head2. next = head1. next . next . next
return head1, head2
if __name__ = = '__main__' :
head1, head2 = formLinkList( None , None )
print ( abs (intersectingNode(head1, head2).data))
|
C#
using System;
public class Node
{
public int data;
public Node next;
public Node( int x)
{
data = x;
next = null ;
}
}
public class GFG{
static Node head1 = null ;
static Node head2 = null ;
static Node intersectingNode(Node headA, Node headB)
{
Node a = headA;
while (a != null )
{
a.data *= -1;
a = a.next;
}
Node b = headB;
while (b != null )
{
if (b.data < 0)
break ;
b = b.next;
}
return b;
}
static void formLinkList()
{
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);
head2 = new Node(10);
head2.next = head1.next.next.next;
return ;
}
static public void Main ()
{
formLinkList();
Console.WriteLine(Math.Abs(
intersectingNode(head1, head2).data));
}
}
|
Javascript
<script>
let head1 = null ;
let head2 = null ;
class Node
{
constructor(x)
{
this .data=x;
this .next= null ;
}
}
function intersectingNode(headA,headB)
{
let a = headA;
while (a != null )
{
a.data *= -1;
a = a.next;
}
let b = headB;
while (b != null )
{
if (b.data < 0)
break ;
b = b.next;
}
return b;
}
function formLinkList()
{
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);
head2 = new Node(10);
head2.next = head1.next.next.next;
return ;
}
formLinkList();
document.write(Math.abs(
intersectingNode(head1, head2).data));
</script>
|
Time Complexity: O(N + M)
Auxiliary Space: O(1)
Last Updated :
28 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...