Identical Linked Lists
Last Updated :
10 Jan, 2023
Two Linked Lists are identical when they have the same data and the arrangement of data is also the same. Write a function to check if the given two linked lists are identical.
Examples:
Input: a = 1->2->3->4, b = 1->2->3->4
Output: Identical
Input: a = 1->3->5->2, b = 1->3->4->6
Output: Not Identical
Check if Linked-Lists are identical using linear traversal:
To solve the problem follow the below idea:
To identify if two lists are identical, we need to traverse both lists simultaneously, and while traversing we need to compare data
- Traverse both the linked lists simultaneously
- If the data of the current node for one linked list is not equal to the node of the other one, then return false
- Return true, as both the linked lists are identical
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
bool areIdentical( struct Node* a, struct Node* b)
{
while (a != NULL && b != NULL) {
if (a->data != b->data)
return false ;
a = a->next;
b = b->next;
}
return (a == NULL && b == NULL);
}
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node
= ( struct Node*) malloc ( sizeof ( struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int main()
{
struct Node* a = NULL;
struct Node* b = NULL;
push(&a, 1);
push(&a, 2);
push(&a, 3);
push(&b, 1);
push(&b, 2);
push(&b, 3);
if (areIdentical(a, b))
cout << "Identical" ;
else
cout << "Not identical" ;
return 0;
}
|
C
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
bool areIdentical( struct Node* a, struct Node* b)
{
while (a != NULL && b != NULL) {
if (a->data != b->data)
return false ;
a = a->next;
b = b->next;
}
return (a == NULL && b == NULL);
}
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node
= ( struct Node*) malloc ( sizeof ( struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int main()
{
struct Node* a = NULL;
struct Node* b = NULL;
push(&a, 1);
push(&a, 2);
push(&a, 3);
push(&b, 1);
push(&b, 2);
push(&b, 3);
areIdentical(a, b) ? printf ( "Identical" )
: printf ( "Not identical" );
return 0;
}
|
Java
import java.io.*;
class LinkedList {
Node head;
class Node {
int data;
Node next;
Node( int d)
{
data = d;
next = null ;
}
}
boolean areIdentical(LinkedList listb)
{
Node a = this .head, b = listb.head;
while (a != null && b != null ) {
if (a.data != b.data)
return false ;
a = a.next;
b = b.next;
}
return (a == null && b == null );
}
void push( int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
public static void main(String args[])
{
LinkedList llist1 = new LinkedList();
LinkedList llist2 = new LinkedList();
llist1.push( 1 );
llist1.push( 2 );
llist1.push( 3 );
llist2.push( 1 );
llist2.push( 2 );
llist2.push( 3 );
if (llist1.areIdentical(llist2) == true )
System.out.println( "Identical " );
else
System.out.println( "Not identical " );
}
}
|
Python3
class Node:
def __init__( self , d):
self .data = d
self . next = None
class LinkedList:
def __init__( self ):
self .head = None
def areIdentical( self , listb):
a = self .head
b = listb.head
while (a ! = None and b ! = None ):
if (a.data ! = b.data):
return False
a = a. next
b = b. next
return (a = = None and b = = None )
def push( self , new_data):
new_node = Node(new_data)
new_node. next = self .head
self .head = new_node
if __name__ = = "__main__" :
llist1 = LinkedList()
llist2 = LinkedList()
llist1.push( 1 )
llist1.push( 2 )
llist1.push( 3 )
llist2.push( 1 )
llist2.push( 2 )
llist2.push( 3 )
if (llist1.areIdentical(llist2) = = True ):
print ( "Identical " )
else :
print ( "Not identical " )
|
C#
using System;
public class LinkedList {
Node head;
public class Node {
public int data;
public Node next;
public Node( int d)
{
data = d;
next = null ;
}
}
bool areIdentical(LinkedList listb)
{
Node a = this .head, b = listb.head;
while (a != null && b != null ) {
if (a.data != b.data)
return false ;
a = a.next;
b = b.next;
}
return (a == null && b == null );
}
void push( int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
public static void Main(String[] args)
{
LinkedList llist1 = new LinkedList();
LinkedList llist2 = new LinkedList();
llist1.push(1);
llist1.push(2);
llist1.push(3);
llist2.push(1);
llist2.push(2);
llist2.push(3);
if (llist1.areIdentical(llist2) == true )
Console.WriteLine( "Identical " );
else
Console.WriteLine( "Not identical " );
}
}
|
Javascript
<script>
class LinkedList{
constructor() {
this .head = null ;
}
}
class Node {
constructor(d) {
this .data = d;
this .next = null ;
}
}
function areIdentical( a,b) {
while (a != null && b != null ) {
if (a.data != b.data)
return false ;
a = a.next;
b = b.next;
}
return (a == null && b == null );
}
function push(root,new_data) {
var new_node = new Node(new_data);
new_node.next = root;
root = new_node;
return root;
}
var llist1 = new LinkedList();
var llist2 = new LinkedList();
llist1 = push(llist1,1);
llist1 = push(llist1,2);
llist1 = push(llist1,3);
llist2 = push(llist2,1);
llist2 = push(llist2,2);
llist2 = push(llist2,3);
if (areIdentical(llist1,llist2) == true )
document.write( "Identical " );
else
document.write( "Not identical " );
</script>
|
Time Complexity: O(min(M, N)). Here, M and N are the sizes of the two linked lists and we have to traverse them till any one of them becomes null. So our time complexity will be a minimum of the size of two linked lists.
Auxiliary Space: O(1). As constant extra space is used.
Above approach using recursion:
Recursive solution code is much cleaner than iterative code. You probably wouldn’t want to use the recursive version for production code, however, because it will use stack space which is proportional to the length of the lists
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node = new Node();
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
bool areIdentical(Node* a, Node* b)
{
if (a == NULL && b == NULL)
return true ;
if (a != NULL && b != NULL)
return (a->data == b->data)
&& areIdentical(a->next, b->next);
return false ;
}
int main()
{
struct Node* a = NULL;
struct Node* b = NULL;
push(&a, 1);
push(&a, 2);
push(&a, 3);
push(&b, 1);
push(&b, 2);
push(&b, 3);
if (areIdentical(a, b))
cout << "Identical" ;
else
cout << "Not identical" ;
return 0;
}
|
C
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node
= ( struct Node*) malloc ( sizeof ( struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
bool areIdentical( struct Node* a, struct Node* b)
{
if (a == NULL && b == NULL)
return true ;
if (a != NULL && b != NULL)
return (a->data == b->data)
&& areIdentical(a->next, b->next);
return false ;
}
int main()
{
struct Node* a = NULL;
struct Node* b = NULL;
push(&a, 1);
push(&a, 2);
push(&a, 3);
push(&b, 1);
push(&b, 2);
push(&b, 3);
areIdentical(a, b) ? printf ( "Identical" )
: printf ( "Not identical" );
return 0;
}
|
Java
import java.io.*;
class LinkedList {
Node head;
class Node {
int data;
Node next;
Node( int d)
{
data = d;
next = null ;
}
}
boolean areIdenticalRecur(Node a, Node b)
{
if (a == null && b == null )
return true ;
if (a != null && b != null )
return (a.data == b.data)
&& areIdenticalRecur(a.next, b.next);
return false ;
}
boolean areIdentical(LinkedList listb)
{
return areIdenticalRecur( this .head, listb.head);
}
void push( int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
public static void main(String args[])
{
LinkedList llist1 = new LinkedList();
LinkedList llist2 = new LinkedList();
llist1.push( 1 );
llist1.push( 2 );
llist1.push( 3 );
llist2.push( 1 );
llist2.push( 2 );
llist2.push( 3 );
if (llist1.areIdentical(llist2) == true )
System.out.println( "Identical " );
else
System.out.println( "Not identical " );
}
}
|
Python3
class Node:
def __init__( self ,data):
self .data = data
self . next = None
def push(head,new_data):
new_node = Node(new_data)
new_node. next = head
head = new_node
return head
def areIdentical(a,b):
if a = = None and b = = None :
return True
if a ! = None and b ! = None :
return a.data = = b.data and areIdentical(a. next ,b. next )
return False
a = None
b = None
a = push(a, 1 )
a = push(a, 2 )
a = push(a, 3 )
b = push(b, 1 )
b = push(b, 2 )
b = push(b, 3 )
if areIdentical(a, b):
print ( "Identical" )
else :
print ( "Not identical" )
|
C#
using System;
public class LinkedList {
Node head;
public class Node {
public int data;
public Node next;
public Node( int d)
{
data = d;
next = null ;
}
}
bool areIdenticalRecur(Node a, Node b)
{
if (a == null && b == null )
return true ;
if (a != null && b != null )
return (a.data == b.data)
&& areIdenticalRecur(a.next, b.next);
return false ;
}
bool areIdentical(LinkedList listb)
{
return areIdenticalRecur( this .head, listb.head);
}
void push( int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
public static void Main(String[] args)
{
LinkedList llist1 = new LinkedList();
LinkedList llist2 = new LinkedList();
llist1.push(1);
llist1.push(2);
llist1.push(3);
llist2.push(1);
llist2.push(2);
llist2.push(3);
if (llist1.areIdentical(llist2) == true )
Console.WriteLine( "Identical " );
else
Console.WriteLine( "Not identical " );
}
}
|
Javascript
<script>
function areIdenticalRecur( a, b)
{
if (a == null && b == null )
return true ;
if (a != null && b != null )
return (a.data == b.data) &&
areIdenticalRecur(a.next, b.next);
return false ;
}
function areIdentical( listb)
{
return areIdenticalRecur( this .head, listb.head);
}
</script>
|
Time Complexity: O(N). Here, N is the length of the smaller list among a and b
Auxiliary Space: O(N). The extra space is used in the recursion call stack.
Please write comments if you find the above codes/algorithms incorrect, or find better ways to solve the same problem.
Share your thoughts in the comments
Please Login to comment...