
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Program to Find the first Common Element between the 2 given Linked Lists
When it is required to find the common element that occurs for the first time between two linked lists, a method to add elements to the linked list, and a method to get the common element that occurs for the first time in these linked lists is defined.
Below is a demonstration for the same −
Example
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 first_common_val(list_1, list_2): curr_1 = list_1.head while curr_1: data = curr_1.data curr_2 = list_2.head while curr_2: if data == curr_2.data: return data curr_2 = curr_2.next curr_1 = curr_1.next return None my_list_1 = LinkedList_structure() my_list_2 = LinkedList_structure() my_list = input('Enter the elements of the first linked list : ').split() for elem in my_list: my_list_1.add_vals(int(elem)) my_list = input('Enter the elements of the second linked list : ').split() for elem in my_list: my_list_2.add_vals(int(elem)) common_vals = first_common_val(my_list_1, my_list_2) if common_vals: print('The element that is present first in the first linked list and is common to both is {}.'.format(common)) else: print('The two lists have no common elements')
Output
Enter the elements of the first linked list : 45 67 89 123 45 Enter the elements of the second linked list : 34 56 78 99 0 11 The two lists have no common elements
Explanation
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’.
A method named ‘add_vals’ is defined, that helps add a value to the stack.
Another method named ‘first_common_val’ is defined, that helps find the first common value that was found in the two linked lists.
Two instances of the ‘LinkedList_structure’ are created.
Elements are added to both the linked lists.
The ‘first_common_value’ method is called on these linked lists.
The output is displayed on the console.
- Related Articles
- Program to find union of two given linked lists in Python
- Program to merge in between linked lists in Python
- Python Program to Check whether 2 Linked Lists are Same
- Program to add two polynomials given as linked lists using Python
- Program to find numbers represented as linked lists in Python
- Python Program to Add Corresponding Positioned Elements of 2 Linked Lists
- Find common elements in three linked lists in C++
- Python program to check if two lists have at least one common element
- Python program to find common elements in three lists using sets
- Python program to print all the common elements of two lists.
- Python Program to Find the Largest Element in a Doubly Linked List
- Python Program to add element to first and last position of linked list
- Program to insert new element into a linked list before the given position in Python
- Golang Program to add the first node in a given linked list.
- Python program to list the difference between two lists.
