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

# Create two linked lists
my_list_1 = LinkedList_structure()
my_list_2 = LinkedList_structure()

# Add elements to first linked list
first_list = [45, 67, 89, 123, 45]
for elem in first_list:
    my_list_1.add_vals(elem)

# Add elements to second linked list  
second_list = [34, 89, 78, 99, 0, 11]
for elem in second_list:
    my_list_2.add_vals(elem)

print("First linked list:", first_list)
print("Second linked list:", second_list)

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_vals))
else:
    print('The two lists have no common elements')
First linked list: [45, 67, 89, 123, 45]
Second linked list: [34, 89, 78, 99, 0, 11]
The element that is present first in the first linked list and is common to both is 89.

How It Works

The algorithm works by traversing the first linked list and for each element, checking if it exists in the second linked list. The first match found is returned as the result.

Example with No Common Elements

# Create new linked lists with no common elements
my_list_3 = LinkedList_structure()
my_list_4 = LinkedList_structure()

# Add elements to first linked list
first_list = [10, 20, 30]
for elem in first_list:
    my_list_3.add_vals(elem)

# Add elements to second linked list  
second_list = [40, 50, 60]
for elem in second_list:
    my_list_4.add_vals(elem)

print("First linked list:", first_list)
print("Second linked list:", second_list)

common_vals = first_common_val(my_list_3, my_list_4)

if common_vals:
    print('The element that is present first in the first linked list and is common to both is {}.'.format(common_vals))
else:
    print('The two lists have no common elements')
First linked list: [10, 20, 30]
Second linked list: [40, 50, 60]
The two lists have no common elements

Explanation

  • The 'Node' class represents each element in the linked list with data and a pointer to the next node.

  • The 'LinkedList_structure' class manages the linked list with methods to add elements.

  • The 'add_vals' method appends new nodes to the end of the linked list.

  • The 'first_common_val' function uses nested loops to compare each element of the first list with all elements of the second list.

  • When a common element is found, it immediately returns that value.

  • If no common elements are found after checking all combinations, it returns None.

Conclusion

This approach finds the first common element between two linked lists with O(m*n) time complexity, where m and n are the lengths of the lists. The algorithm prioritizes the order of elements in the first list to determine which common element appears "first".

Updated on: 2026-03-25T18:38:37+05:30

347 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements