I was trying LinkedLists on java on a online platform. It cleared 7 of 8 cases. The 4th case did not clear. The question was to compare two linked lists and print 1 if all corresponding elements are equal and 0 otherwise. For this test case the expected output is 1 as all elements are equal but I got 0. I have included some extra prints of linked lists and comparisons I did to understand it. Well as it cleared 7 of 8 cases I would like to understand what is going wrong with this particular test case. Thank you in advance.
import java.util.LinkedList;
import java.util.Scanner;
public class compare_two_ll {
public static void main(String[] args) {
// initialising and taking inputs
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
int n = sc.nextInt();
LinkedList<Integer> l1 = new LinkedList<Integer>();
for(int i=0;i<m;i++){
l1.add(sc.nextInt());
}
LinkedList<Integer> l2 = new LinkedList<Integer>();
for(int i=0;i<n;i++){
l2.add(sc.nextInt());
}
// printing two linked lists
for(Integer x:l1){
System.out.print(x);
System.out.print(" ");
}
System.out.println();
for(Integer x:l2){
System.out.print(x);
System.out.print(" ");
}
System.out.println();
boolean flag = true;
if(m==n){
for(int i=0;i<n;i++){
if(l1.get(i)!=l2.get(i)){
// printing few next 4 lines trying to understand problem
System.out.println(l1.get(i)==l1.get(i));
System.out.println(Integer.valueOf(l1.get(i)) == Integer.valueOf(l2.get(i)));
System.out.println(l1.get(i));
System.out.println(l2.get(i));
System.out.println(0);
flag = false;
break;
}
}
if(flag){
System.out.println(1);
}
}else{
System.out.println(0);
}
sc.close();
}
}
The 4th test case was:
20 20 736 24 780 497 255 361 943 5 406 542 843 17 10 323 828 552 301 301 376 250 736 24 780 497 255 361 943 5 406 542 843 17 10 323 828 552 301 301 376 250
And output:
736 24 780 497 255 361 943 5 406 542 843 17 10 323 828 552 301 301 376 250
736 24 780 497 255 361 943 5 406 542 843 17 10 323 828 552 301 301 376 250
true
false
736
736
0