Identical Linked Lists | Practice | GeeksforGeeks

Given two Singly Linked List of N and M nodes respectively. The task is to check whether two linked lists are identical or not. 
Two Linked Lists are identical when they have same data and with same arrangement too.
Example 1:
Input:
LinkedList1: 1->2->3->4->5->6
LinkedList2: 99->59->42->20
Output: Not identical 
Example 2:
Input:
LinkedList1: 1->2->3->4->5
LinkedList2: 1->2->3->4->5
Output: Identical
 

Your Task:
The task is to complete the function areIdentical() which takes the head of both linked lists as arguments and returns True or False.
Constraints:
1 <= N <= 103
Expected Time Complexity : O(N)
Expected Auxilliary Space : O(1)
0
durgeshownlabin 10 hours
// java code 100% accurate

class Solution {
    
    //Function to check whether two linked lists are identical or not.
    public boolean isIdentical (Node head1, Node head2){
        //write your code here 
        Node h1=head1;
        Node h2=head2;
        
        while(h1!=null || h2!=null)
        {
            if(h1!=null && h2!=null)
            {
                if(h1.data==h2.data)
                {
                    h1=h1.next;
                    h2=h2.next;
                    continue;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        return true;
    }
}
0
aditrj952 days ago
bool areIdentical(struct Node *head1, struct Node *head2){   Node*curr1=head1;   Node*curr2=head2;    while(curr1!=NULL||curr2!=NULL){      if(curr1==NULL&&curr2!=NULL){          return false;      }      if(curr1!=NULL&&curr2==NULL){          return false;      }      if(curr1->data==curr2->data){       curr1=curr1->next;         curr2=curr2->next;         }                                 else         return false;  }  return true;}
0
neelasatyasai936 days ago
#PYTHON
def areIdentical(head1, head2):
   # Code here
   temp1=head1
   temp2=head2
   while temp1:
        if temp1.data!=temp2.data :
          return 0
        temp1=temp1.next
        temp2=temp2.next
   return 1
0
rajeshsurana1 week ago
Very simple JAVA solution →
    public boolean isIdentical (Node head1, Node head2){
        Node curr1 = head1, curr2 = head2;
        while (curr1 != null && curr2 != null && curr1.data == curr2.data) {
            curr1 = curr1.next; curr2 = curr2.next;
        }
        return curr1 == null && curr2 == null;
    }
+1
shahabuddinbravo402 weeks ago
bool areIdentical(struct Node *head1, struct Node *head2){  Node *count1=head1,*count2=head2;    int N=0,M=0;  while(count1!=NULL){      count1=count1->next;      N++;  }    while(count2!=NULL){      count2=count2->next;      M++;  }    if(M==N){      Node *ptr1=head1,*ptr2=head2;      while(ptr1!=NULL && ptr2!=NULL){          if(ptr1->data!=ptr2->data){              return false;          }          ptr1=ptr1->next;          ptr2=ptr2->next;      }  }  else{      return false;  }    return true;}
0
akashmarkad22102 weeks ago
// JAVA CODE
 
public boolean isIdentical (Node head1, Node head2){       //write your code here               if(head1 == null || head2 == null)       {           return false;       }                     while(head1 != null && head2 != null)       {           if(head1.data != head2.data)           {               return false;           }           else           {               head1 = head1.next;               head2 = head2.next;           }       }              if(head1 == null && head2 == null)       {           return true;       }                 return false;   }
0
rajstormo2 weeks ago
Algorithm
Take two pointers of Node type, p1 which is pointing to head1 and pointer p2 pointing to head2
Do this till both p1 and p2 are pointing to some Nodecheck if p1→data is equal to p2 → data, if not return false , otherwise continue
check if p1→data is equal to p2 → data, if not return false , otherwise continue
Now, if any of the pointers p1 and p2 is still pointing to some Node, return false as it contains extra Nodes and hence both the Linked lists are not identical
At last return true, if none of the above case matches that means the LL are identical
0
rajstormo2 weeks ago
C++ Solution 
bool areIdentical(struct Node *head1, struct Node *head2){   Node *t1 = head1;   Node *t2 = head2;   while (t1 != NULL && t2 != NULL) {       if (t1 -> data != t2 -> data)           return false;       t1 = t1 -> next;       t2 = t2 -> next;   }   if (t1 != NULL || t2 != NULL)       return false;   return true;}
0
hharshit81183 weeks ago
bool areIdentical(struct Node *a, struct Node *b){  if((a && !b) || (!a && b)){    return false;}while(a && b){    if(a->data != b->data){        return false;    }    a = a->next;    b = b->next;}if(a != NULL || b!= NULL){    return false;}return true;}
0
ankitmaurya02253 weeks ago
Easy and Simple Approach O(N) :-
bool areIdentical(struct Node *head1, struct Node *head2){   // Code here  struct Node *curr1 = head1;  struct Node *curr2 = head2;   int len1=1,len2=1;   while(curr1->next!=NULL)   {       len1++;       curr1=curr1->next;   }   while(curr2->next!=NULL)   {       len2++;       curr2=curr2->next;   }   if(len1!=len2)   return false;   while(head1!=NULL && head2!=NULL)   {       if(head1->data!=head2->data)       return false;       head1=head1->next;       head2=head2->next;   }   return true;}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.