Description: You are given pointers to two Linked Lists. Your task is to identify whether they are identical or not.(Two lists are identical if they contain same number of nodes with same data and arrangement.)

 

If video does not load, Click here

Algorithm:

1)  Loop through both the Linked List

a.  If both point to NULL, return true

b.  If only one of them points to Null return false

c.  If their data is not same return false

d.  Else move a step ahead

 

C Implementation:

 
int Identical(struct node *a, struct node *b)
{/* Loop through both the Linked List */
  while(1)
  {
  /* If both point to NULL, return true */
    if(a == NULL && b == NULL)    
    {  return 1; }
/* If only one of them points to Null return false */
    if(a == NULL && b != NULL) 
    {  return 0; }
   if(a != NULL && b == NULL) 
    {  return 0; }
/* If their data is not same return false */
    if(a->data != b->data)
    {  return 0; }
/* Else move a step ahead */
    a = a->next;
    b = b->next;
  }         
}    

 

Time Complexity:

O(n)

Go to top