Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

C# Program For Finding Intersection Of Two Sorted Linked Lists

  • Last Updated : 15 Dec, 2021

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:


// C# program to implement 
// the above approach
using System;
public class GFG 
    // Dummy node for storing 
    // intersection
    static Node dummy = null;
    // Tail node for keeping track of 
    // last node so that it makes easy 
    // for insertion
    static Node tail = null;
    // class - Node
    public  class Node
        public int data;
        public  Node next;
        public  Node(int data)
   = data;
            next = null;
    // Head nodes for pointing to 1st 
    // and 2nd linked lists
    Node a = null, b = null;
    // Function for printing the list
    void printList(Node start)
        Node p = start;
        while (p != null)
            Console.Write( + " ");
            p =;
    // Inserting elements into list
    void push(int data)
        Node temp = new Node(data);
        if(dummy == null)
            dummy = temp;
            tail = temp;
   = temp;
            tail = temp;
    // Function for finding intersection 
    // and adding it to dummy list 
    void sortedIntersect()
        // Pointers for iterating
        Node p = a,q = b;
        while(p != null  &&  q != null)
            if( ==
                // Add to dummy list
                p =;
                q =;
            else if( <
                p =;
    // Driver code
    public static void Main(String []args)
        GFG list = new GFG();
        // Creating first linked list
        list.a = new Node(1); = new Node(2); = new Node(3); = new Node(4); = new Node(6);
        // Creating second linked list
        list.b = new Node(2); = new Node(4); = new Node(6); = new Node(8);
        // Function call for intersection
        // Print required intersection
                "Linked list containing common items of a & b");
// This code is contributed by aashish1995


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!