Topic: Linked List
Questions Successfully Completed: 1
1) Identical Linked Lists | Easy |
Identical Linked Lists
Time Complexity : O(N)
Space Complexity : O(1)
Question
Input: LinkedList1: 1->2->3->4->5->6 LinkedList2: 99->59->42->20 Output: Not identical
class Node {
int data;
Node next;
public Node(int data){
this.data = data;
this.next = null;
}
}
class Solution {
//Function to check whether two linked lists are identical or not.
public boolean isIdentical (Node head1, Node head2){
Node h1 = head1;
Node h2 = head2;
int h11=0;
int h22=0;
while(h1!=null){
h11++;
h1 = h1.next;
}
while(h2!=null){
h22++;
h2 = h2.next;
}
if(h22!=h11){return false;}
while(head1!=null){
if(head1.data!=head2.data){
return false;
}
head1 = head1.next;
head2 = head2.next;
}
return true;
}
}
Thank you for reading :)