HomePythonPython Program to Find Intersection and Union of Two Linked Lists

Python Program to Find Intersection and Union of Two Linked Lists

In this Python program, we will be implementing the intersection and union operations on two linked lists. Linked lists are data structures that consist of a sequence of elements, where each element points to the next element in the sequence.

Problem Statement

Given two linked lists, we need to find the intersection and union of these lists.

Python Program to Find Intersection and Union of Two Linked Lists

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class LinkedList:
    def __init__(self):
        self.head = None

    def insert(self, data):
        new_node = Node(data)
        if not self.head:
            self.head = new_node
            current = self.head
            while current.next:
                current = current.next
            current.next = new_node

    def display(self):
        current = self.head
        while current:
            print(current.data, end=" -> ")
            current = current.next

    def find_intersection(self, other_list):
        intersection = LinkedList()
        current = self.head
        while current:
            if other_list.search(current.data):
            current = current.next
        return intersection

    def search(self, data):
        current = self.head
        while current:
            if current.data == data:
                return True
            current = current.next
        return False

    def find_union(self, other_list):
        union = LinkedList()
        current = self.head

        while current:
            current = current.next

        other_current = other_list.head
        while other_current:
            if not union.search(other_current.data):
            other_current = other_current.next

        return union

list1 = LinkedList()

list2 = LinkedList()

print("Linked List 1:")

print("Linked List 2:")

intersection_result = list1.find_intersection(list2)

union_result = list1.find_union(list2)

How It Works

  1. In this program, we define a Node class to represent elements in the linked list and a LinkedList class to manage the linked list operations.
  2. The find_intersection method iterates through the first list and checks if each element is present in the second list.
  3. If yes, it adds the element to the intersection linked list.
  4. The find_union method combines the elements of both lists while avoiding duplicates.
  5. The search method is used to search for an element in the linked list.

Input / Output

Python Program to Find Intersection and Union of Two Linked Lists

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this Python program, we will create a singly linked list and remove duplicate elements from it. A linked list...
This Python program solves the Celebrity Problem by finding a person who is known by everyone but does not know...
This Python program uses a recursive approach to solve the n-Queens problem. It explores all possible combinations of queen placements...