Given two singly linked lists. The task is to determine if the two linked lists are identical or not. Two linked lists are considered identical if they contain the same data in the same order. If the linked lists are identical, return true otherwise, return false.
[Expected Approach] By Traversal of Linked List - O(n) time and O(1) Space:
The idea is to traverse both the lists simultaneously and while traversing we need to compare data.
Code Implementation:
C++
// C++ program to check if two linked lists// are identical or not#include<bits/stdc++.h>usingnamespacestd;structNode{intdata;Node*next;Node(intval){data=val;next=nullptr;}};// Returns true if linked lists head1 and head2 are// identical, otherwise falseboolareIdentical(Node*head1,Node*head2){while(head1!=nullptr&&head2!=nullptr){if(head1->data!=head2->data)returnfalse;// Move to next nodes in both lists head1=head1->next;head2=head2->next;}// If linked lists are identical,// both 'head1' and 'head2' must be NULLreturn(head1==nullptr&&head2==nullptr);}intmain(){// Create first linked list: 3 -> 2 -> 1Node*head1=newNode(3);head1->next=newNode(2);head1->next->next=newNode(1);// Create second linked list: 3 -> 2 -> 1Node*head2=newNode(3);head2->next=newNode(2);head2->next->next=newNode(1);// Function callif(areIdentical(head1,head2))cout<<"True";elsecout<<"False";return0;}
C
// C program to check if two linked lists// are identical or not#include<stdio.h>structNode{intdata;structNode*next;};// Returns true if linked lists head1 and head2 are // identical, otherwise falseintareIdentical(structNode*head1,structNode*head2){while(head1!=NULL&&head2!=NULL){if(head1->data!=head2->data)return0;// Move to next nodes in both listshead1=head1->next;head2=head2->next;}// If linked lists are identical, // both 'head1' and 'head2' must be NULLreturn(head1==NULL&&head2==NULL);}structNode*createNode(intval){structNode*new_node=(structNode*)malloc(sizeof(structNode));new_node->data=val;new_node->next=NULL;returnnew_node;}intmain(){// Create first linked list: 3 -> 2 -> 1structNode*head1=createNode(3);head1->next=createNode(2);head1->next->next=createNode(1);// Create second linked list: 3 -> 2 -> 1structNode*head2=createNode(3);head2->next=createNode(2);head2->next->next=createNode(1);// Function callif(areIdentical(head1,head2))printf("True\n");elseprintf("False\n");return0;}
Java
// Java program to check if two linked lists// are identical or notclassNode{intdata;Nodenext;Node(intval){data=val;next=null;}}classGfG{// Returns true if linked lists head1 and head2 are// identical, otherwise falsestaticbooleanareIdentical(Nodehead1,Nodehead2){while(head1!=null&&head2!=null){if(head1.data!=head2.data)returnfalse;/* Move to next nodes in both lists */head1=head1.next;head2=head2.next;}// If linked lists are identical,// both 'head1' and 'head2' must be NULLreturn(head1==null&&head2==null);}publicstaticvoidmain(String[]args){// Create first linked list: 3 -> 2 -> 1Nodehead1=newNode(3);head1.next=newNode(2);head1.next.next=newNode(1);// Create second linked list: 3 -> 2 -> 1Nodehead2=newNode(3);head2.next=newNode(2);head2.next.next=newNode(1);// Function callif(areIdentical(head1,head2))System.out.println("True");elseSystem.out.println("False");}}
Python
# Python program to check if two linked lists# are identical or notclassNode:def__init__(self,val):self.data=valself.next=None# Returns true if linked lists head1 and head2 are# identical, otherwise falsedefareIdentical(head1,head2):whilehead1isnotNoneandhead2isnotNone:ifhead1.data!=head2.data:returnFalse# Move to next nodes in both listshead1=head1.nexthead2=head2.next# If linked lists are identical,# both 'head1' and 'head2' must be Nonereturnhead1isNoneandhead2isNoneif__name__=="__main__":# Create first linked list: 3 -> 2 -> 1head1=Node(3)head1.next=Node(2)head1.next.next=Node(1)# Create second linked list: 3 -> 2 -> 1head2=Node(3)head2.next=Node(2)head2.next.next=Node(1)# Function callifareIdentical(head1,head2):print("True")else:print("False")
C#
// C# program to check if two linked lists// are identical or notusingSystem;publicclassNode{publicintData;publicNodeNext;publicNode(intval){Data=val;Next=null;}}classGfG{// Returns true if linked lists head1 and head2 are// identical, otherwise falsestaticboolAreIdentical(Nodehead1,Nodehead2){while(head1!=null&&head2!=null){if(head1.Data!=head2.Data)returnfalse;// Move to next nodes in both listshead1=head1.Next;head2=head2.Next;}// If linked lists are identical,// both 'head1' and 'head2' must be NULLreturn(head1==null&&head2==null);}publicstaticvoidMain(string[]args){// Create first linked list: 3 -> 2 -> 1Nodehead1=newNode(3);head1.Next=newNode(2);head1.Next.Next=newNode(1);// Create second linked list: 3 -> 2 -> 1Nodehead2=newNode(3);head2.Next=newNode(2);head2.Next.Next=newNode(1);if(AreIdentical(head1,head2))Console.WriteLine("True");elseConsole.WriteLine("False");}}
JavaScript
// JavaScript program to check if two linked lists// are identical or notclassNode{constructor(val){this.data=val;this.next=null;}}// Returns true if linked lists head1 and head2 are // identical, otherwise falsefunctionareIdentical(head1,head2){while(head1!==null&&head2!==null){if(head1.data!==head2.data){returnfalse;}// Move to next nodes in both listshead1=head1.next;head2=head2.next;}// If linked lists are identical, // both 'head1' and 'head2' must be nullreturnhead1===null&&head2===null;}// Create first linked list: 3 -> 2 -> 1lethead1=newNode(3);head1.next=newNode(2);head1.next.next=newNode(1);// Create second linked list: 3 -> 2 -> 1lethead2=newNode(3);head2.next=newNode(2);head2.next.next=newNode(1);if(areIdentical(head1,head2)){console.log("True");}else{console.log("False");}
Output
True
Time Complexity: O(n), Here n is the length of the smaller list among two linked list. Auxiliary Space: O(1).