Intersection point of two linked lists


What is the Error in My Code?

Compiling failed with exitcode 1, compiler output: Main.java:208: error: ‘)’ expected public int search(int N, int M node p1, Node q1){ ^ Main.java:208: error: expected public int search(int N, int M node p1, Node q1){ ^ Main.java:208: error: ‘;’ expected public int search(int N, int M node p1, Node q1){ ^ Main.java:208: error: expected public int search(int N, int M node p1, Node q1){ ^ Main.java:222: error: class, interface, or enum expected public static void main(String[] args) throws Exception { ^ Main.java:225: error: class, interface, or enum expected int N = scn.nextInt(); ^ Main.java:226: error: class, interface, or enum expected int M = scn.nextInt(); ^ Main.java:229: error: class, interface, or enum expected LinkedList list1 = new LinkedList(); ^ Main.java:231: error: class, interface, or enum expected for (int i = 0; i < N; i++) { ^ Main.java:231: error: class, interface, or enum expected for (int i = 0; i < N; i++) { ^ Main.java:231: error: class, interface, or enum expected for (int i = 0; i < N; i++) { ^ Main.java:233: error: class, interface, or enum expected } ^ Main.java:236: error: class, interface, or enum expected for (int i = 0; i < M; i++) { ^ Main.java:236: error: class, interface, or enum expected for (int i = 0; i < M; i++) { ^ Main.java:236: error: class, interface, or enum expected for (int i = 0; i < M; i++) { ^ Main.java:238: error: class, interface, or enum expected } ^ Main.java:240: error: class, interface, or enum expected Node q1=new Node(list2.head.data, list2.head); ^ Main.java:241: error: class, interface, or enum expected int a=list1.search(list2, N, M, p1, q1); ^ Main.java:242: error: class, interface, or enum expected System.out .println(a); ^ Main.java:243: error: class, interface, or enum expected }

HI @dktrip,
There are typos in your code and also the way you create a new node to navigate is wrong … And i think you did not understand the question … It is not necessary that 4 element of two linked list intersect .The intersection element may be the third element of first and fifth of second linked list … when you do if(p1.data==q1.data) in its else statement you are incrementing both p1 and q1 which is not necessary true … Like in test case also
5
10 20 30 40 50
4
15 25 40 50

40 is the fourth element of first and the third element of second linkedlist …
check and modify your code

I have made yhe required changes.

You did not understand … what if the test case was
5
10 20 30 40 50
4
40 25 10 50
then your code would not have worked … you need to do simple linear search in a linked list … take the first element of first linked list and compare with every other element in second link list … if no common is found then take the second element of first linked list and compare again with every element …

But how are your linked lists forming Y-shaped linked lists?
Also your method increases the Time Complexity.

My method is increasing time complexity but your method is wrong … Hence we do not care if your time complexity is less because the answer that it gives is wrong … and it is forming Y because after the common element all the elements are same …According to your code all the element before the common element are same.
6
10 20 30 40 50 70
3
40 50 70
Test case like this …Sorry for the above test case that was supposed to give a intution of a completly different thing but your doubt is different so consider this test case … After 40 all elements are same.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.