Intersection point of two Linked Lists | Set 3 | C ++ program for the above approach ; Structure of a node of a Linked List ; Constructor ; Function to find the intersection point of the two Linked Lists ; Traverse the first linked list and multiply all values by - 1 ; Update a -> data ; Update a ; Traverse the second Linked List and find the value of the first node having negative value ; Intersection point ; Update b ; Function to create linked lists ; Linked List L1 ; Linked List L2 ; Driver Code
#include <bits/stdc++.h> NEW_LINE 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 ; }