Intersection point of two Linked Lists | Set 3 | Java 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
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 ) ) ; } }