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
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
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 " )
|
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
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
|
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!
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...