Skip to content
Related Articles

Related Articles

Python Program For Finding Intersection Of Two Sorted Linked Lists

View Discussion
Improve Article
Save Article
  • Last Updated : 15 Dec, 2021
View Discussion
Improve Article
Save Article

Given two lists sorted in increasing order, create and return a new list representing the intersection of the two lists. The new list should be made with its own memory — the original lists should not be changed. 


First linked list: 1->2->3->4->6
Second linked list be 2->4->6->8, 
Output: 2->4->6.
The elements 2, 4, 6 are common in 
both the list so they appear in the 
intersection list. 

First linked list: 1->2->3->4->5
Second linked list be 2->3->4, 
Output: 2->3->4
The elements 2, 3, 4 are common in 
both the list so they appear in the 
intersection list.

Method: Using Dummy Node. 
The idea is to use a temporary dummy node at the start of the result list. The pointer tail always points to the last node in the result list, so new nodes can be added easily. The dummy node initially gives the tail a memory space to point to. This dummy node is efficient, since it is only temporary, and it is allocated in the stack. The loop proceeds, removing one node from either ‘a’ or ‘b’ and adding it to the tail. When the given lists are traversed the result is in dummy. next, as the values are allocated from next node of the dummy. If both the elements are equal then remove both and insert the element to the tail. Else remove the smaller element among both the lists. 

Below is the implementation of the above approach:


# Python program to implement
# the above approach
# Link list node 
class Node:
    def __init__(self):   = 0 = None
''' This solution uses the temporary
    dummy to build up the result list '''
def sortedIntersect(a, b):
    dummy = Node()
    tail = dummy; = None;
    ''' Once one or the other 
        list runs out -- we're done '''
    while (a != None and b != None):
        if ( ==
   = push((, 
            tail =;
            a =;
            b =;
        # Advance the smaller list
        elif( <
            a =;
            b =;    
    return (;
''' Function to insert a node at 
    the beginning of the linked list '''
def push(head_ref, new_data):
    # Allocate node 
    new_node = Node()
    # Put in the data = new_data;
    # Link the old list off the new node = (head_ref);
    # Move the head to point to the
    # new node 
    (head_ref) = new_node;    
    return head_ref
''' Function to print nodes in 
    a given linked list '''
def printList(node):
    while (node != None):
        print(, end = ' ')
        node =;
# Driver code
if __name__=='__main__':
    # Start with the empty lists 
    a = None;
    b = None;
    intersect = None;
    ''' Let us create the first sorted 
        linked list to test the functions
        Created linked list will be '''
    a = push(a, 6);
    a = push(a, 5);
    a = push(a, 4);
    a = push(a, 3);
    a = push(a, 2);
    a = push(a, 1);
    ''' Let us create the second sorted 
        linked list. Created linked list 
        will be '''
    b = push(b, 8);
    b = push(b, 6);
    b = push(b, 4);
    b = push(b, 2);
    # Find the intersection two linked lists 
    intersect = sortedIntersect(a, b);
    print("Linked list containing common items of a & b ");
# This code is contributed by rutvik_56.


Linked list containing common items of a & b 
2 4 6 

Complexity Analysis: 

  • Time Complexity: O(m+n) where m and n are number of nodes in first and second linked lists respectively. 
    Only one traversal of the lists are needed.
  • Auxiliary Space: O(min(m, n)). 
    The output list can store at most min(m,n) nodes .

Please refer complete article on Intersection of two Sorted Linked Lists for more details!

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!