- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to Check whether 2 Linked Lists are Same
When it is required to check if two linked lists are same, a method to add elements to the linked list, and a method to check the equality of the elements in the linked lists are defined.
Below is a demonstration for the same −
class Node: def __init__(self, data): self.data = data self.next = None class LinkedList_structure: def __init__(self): self.head = None self.last_node = None def add_vals(self, data): if self.last_node is None: self.head = Node(data) self.last_node = self.head else: self.last_node.next = Node(data) self.last_node = self.last_node.next def check_equality(list_1, list_2): curr_1 = list_1.head curr_2 = list_2.head while (curr_1 and curr_2): if curr_1.data != curr_2.data: return False curr_1 = curr_1.next curr_2 = curr_2.next if curr_1 is None and curr_2 is None: return True else: return False my_linked_list_1 = LinkedList_structure() my_linked_list_2 = LinkedList_structure() my_list = input('Enter the elements of the first linked list: ').split() for elem in my_list: my_linked_list_1.add_vals(int(elem)) my_list = input('Enter the elements of the second linked list: ').split() for elem in my_list: my_linked_list_2.add_vals(int(elem)) if check_equality(my_linked_list_1, my_linked_list_2): print('The two linked lists are the same') else: print('The two linked list are not same')
Enter the elements of the first linked list: 34 56 89 12 45 Enter the elements of the second linked list: 57 23 78 0 2 The two linked list are not same
The ‘Node’ class is created.
Another ‘LinkedList_structure’ class with required attributes is created.
It has an ‘init’ function that is used to initialize the first element, i.e the ‘head’ to ‘None’ and ‘last_node’ to ‘None’.
A method named ‘add_vals’ is defined, that helps add a value to the stack.
Another method named ‘check_equality’ is defined, that helps check if elements in two linked list are same or not.
It returns True or False depending on the equality.
Two instances of the ‘LinkedList_structure’ are created.
Elements are added to the two linked lists.
The ‘check_equality’ method is called on these two linked lists.
The output is displayed on the console.
- Python program to check whether two lists are circularly identical
- Python Program to Add Corresponding Positioned Elements of 2 Linked Lists
- Program to check whether all leaves are at same level or not in Python
- Python Program to Find the first Common Element between the 2 given Linked Lists
- C# program to check whether two sequences are the same or not
- Program to check whether leaves sequences are same of two leaves or not in python
- Python Program to Check whether a Singly Linked List is a Palindrome
- Program to check whether there is any pair of words which are almost same in Python
- Program to merge in between linked lists in Python
- Program to check whether String Halves Are Alike in Python
- Program to find numbers represented as linked lists in Python
- Program to check whether parentheses are balanced or not in Python
- Program to interleave list elements from two linked lists in Python
- Program to find union of two given linked lists in Python
- Program to add two polynomials given as linked lists using Python