# Description :-

Given two singly linked lists of size N and M, write a program to get the point where two linked lists intersect each other.

Example 1:

`Input:LinkList1 = 3->6->9->commonLinkList2 = 10->commoncommon = 15->30->NULLOutput: 15`

The task is to complete the function which takes the pointer to the head of linklist1(head1) and linklist2(head2) as input parameters and returns data value of a node where two linked lists intersect. If linked list do not merge at any point, then it should return -1.

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

Constraints:
1 <= N <= 100
1 <= value <= 1000

# Solution:-

Step 2 : If a pointer is null them set point a pointer to head of linkedlist2.If not, then move a pointer to next node.

Step 3 : If b pointer is null them set point a pointer to head of linkedlist1.If not, then move b pointer to next node.

Step 4: Continue step 2 and step 3 until a pointer is not equal to b pointer. Finally, return the value of data contained in node pointed by either a pointer or b pointer.

Below is the java solution for this problem:

`class Intersect{ public int intersectPoint(Node headA, Node headB){        Node a=headA;        Node b=headB;                while(a!=b){                        if(a==null){                a=headB;            }else{                a=a.next;            }            if(b==null){                b=headA;            }else{                b=b.next;            }        }         return a.data;  }}`

--

--

--

Love writing technical articles

Love podcasts or audiobooks? Learn on the go with our new app.

## Kriti Mittal

Love writing technical articles