Python Program To Check If Two Linked Lists Are Identical
Last Updated :
23 Apr, 2023
Two Linked Lists are identical when they have the same data and the arrangement of data is also the same. For example, Linked lists a (1->2->3) and b(1->2->3) are identical. . Write a function to check if the given two linked lists are identical.
Method 1 (Iterative):
To identify if two lists are identical, we need to traverse both lists simultaneously, and while traversing we need to compare data.
Python3
# An iterative Java program to check if
# two linked lists are identical or not
# Linked list Node
class Node:
def __init__(self, d):
self.data = d
self.next = None
class LinkedList:
def __init__(self):
# Head of list
self.head = None
# Returns true if linked lists a
# and b are identical, otherwise false
def areIdentical(self, listb):
a = self.head
b = listb.head
while (a != None and b != None):
if (a.data != b.data):
return False
# If we reach here, then a and b
# are not null and their data is
# same, so move to next nodes
# in both lists
a = a.next
b = b.next
# If linked lists are identical,
# then 'a' and 'b' must be null
# at this point.
return (a == None and b == None)
# UTILITY FUNCTIONS TO TEST fun1()
# and fun2()
# Given a reference (pointer to pointer)
# to the head of a list and an int, push
# a new node on the front of the list.
def push(self, new_data):
# 1 & 2: Allocate the Node &
# Put in the data
new_node = Node(new_data)
# 3. Make next of new Node as head
new_node.next = self.head
# 4. Move the head to point to
# new Node
self.head = new_node
# Driver Code
llist1 = LinkedList()
llist2 = LinkedList()
# The constructed linked lists are :
# llist1: 3->2->1
# llist2: 3->2->1
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 ")
# This code is contributed by Prerna Saini
Output:
Identical
Time Complexity: O(n)
Method 2 (Recursive):
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.
Python3
# A recursive Python3 function to check
# if two linked lists are identical
# or not
def areIdentical(a, b):
# If both lists are empty
if (a == None and b == None):
return True
# If both lists are not empty,
# then data of current nodes must
# match, and same should be recursively
# true for rest of the nodes.
if (a != None and b != None):
return ((a.data == b.data) and
areIdentical(a.next, b.next))
# If we reach here, then one of the lists
# is empty and other is not
return False
# This code is contributed by Princi Singh
Time Complexity: O(n) for both iterative and recursive versions. n is the length of the smaller list among a and b.
Auxiliary Space: O(n) for call stack because using recursion
Approach 3: Hash Table:
- Another approach is to compare two linked lists is a hash table or a set. The idea is to traverse the first linked list and insert each node's value into the hash table or set.
- Then, traverse the second linked list and check if each node's value exists in the hash table or set. If a node's value is not found in the hash table or set, then the linked lists are not identical.
Here's a simple Python Code that uses a hash table (unordered_set) to compare two linked lists:
Python3
class Node:
def __init__(self, data):
self.data = data
self.next = None
def areIdentical(head1, head2):
s = set()
while head1 != None:
s.add(head1.data)
head1 = head1.next
while head2 != None:
if head2.data not in s:
return False
head2 = head2.next
return True
def push(head, data):
new_node = Node(data)
new_node.next = head
head = new_node
return head
if __name__ == '__main__':
head1 = None
head2 = None
head1 = push(head1, 1)
head1 = push(head1, 2)
head1 = push(head1, 3)
head2 = push(head2, 1)
head2 = push(head2, 2)
head2 = push(head2, 3)
if areIdentical(head1, head2):
print("Identical")
else:
print("Not identical")
Time Complexity: O(n) where n is the length of the smaller list among a and b.
Space complexity: O(n), where n is the number of elements stored in the table.
Please refer complete article on Identical Linked Lists for more details!
Similar Reads
Check if two lists are identical in Python Our task is to check if two lists are identical or not. By "identical", we mean that the lists contain the same elements in the same order. The simplest way to check if two lists are identical using the equality operator (==).Using Equality Operator (==)The easiest way to check if two lists are iden
2 min read
Javascript Program To Check If Two Linked Lists Are Identical Two Linked Lists are identical when they have the same data and the arrangement of data is also the same. For example, Linked lists a (1->2->3) and b(1->2->3) are identical. . Write a function to check if the given two linked lists are identical.Method (Recursive): Recursive solution cod
3 min read
Check if two given matrices are identical - Python In Python, we often need to check whether two given matrices are identical. Two matrices are considered identical if they have the same number of rows and columns and the corresponding elements in each matrix are equal. Let's explore various methods to compare two matrices and check if they are iden
3 min read
Check if a List is Sorted or not - Python We are given a list of numbers and our task is to check whether the list is sorted in increasing or decreasing order. For example, if the input is [1, 2, 3, 4], the output should be True, but for [3, 1, 2], it should be False.Using all()all() function checks if every pair of consecutive elements in
3 min read
How to Compare List of Dictionaries in Python Comparing a list of dictionaries in Python involves checking if the dictionaries in the list are equal, either entirely or based on specific key-value pairs. This process helps to identify if two lists of dictionaries are identical or have any differences. The simplest approach to compare two lists
3 min read
Check Whether Two Lists are Circularly Identical We are given two lists of elements, and the task is to check if one list is a circular rotation of the other. For example, the lists [10, 10, 10, 0, 0] and [10, 10, 10, 0, 0] are circularly identical because the second list can be obtained by rotating the first list. Let's discuss various ways to pe
3 min read