COMPARE TWO LINKED LIST HACKKERANK
static boolean compareLists(SinglyLinkedListNode head1, SinglyLinkedListNode head2) {
SinglyLinkedListNode temp1=head1;
SinglyLinkedListNode temp2=head2;
int count1=0;
int count2=0;
int m=0;
int count3=0;
while(temp1!=null){
count1++;
temp1=temp1.next;
}
/*while(temp2!=null){
count2++;
temp2=temp2.next;
}*/
/*if(count1!=count2){
return false;
}
else{*/
temp1=head1;
temp2=head2;
while(temp1!=null && temp2!=null){
if(temp1.data==temp2.data){
count3++;
temp1=temp1.next;
temp2=temp2.next;
}
else {
count3=0;
break;
}
}
if(count3==count1){
return true;
}
else
return false;
//}
/*if(m==1){
return true;
}
else
return false;*/
}
/*FOR better time complexity don't traverse both linked lists so as to find the
length of both linked lists. Instead, just traverse one linked list in order to match
the length with the count3 ie the total number of values same in both lists. */
Comments
Post a Comment