Intersection of two Sorted Linked Lists

Last Updated : 7 Sep, 2026

Given two singly linked lists head1 and head2, where both lists are sorted in increasing order, find their intersection and create a new linked list containing all the common elements.

  • If an element occurs multiple times in both lists, it should appear in the intersection as many times as it occurs in both lists.
  • The original linked lists should not be modified.

Example: 

Input: head1 = 1 -> 2 -> 3 -> 4 -> 6, head2 = 2 -> 4 -> 6 -> 8
Output: 2 -> 4-> 6
Explanation: The elements 2, 4, and 6 are common in both lists, so they appear in the intersection list.

linkedlist_1

Input: head1 = 1 -> 2 -> 2 -> 3 -> 4, head2 = 2 -> 2 -> 2 -> 4 -> 5
Output: 2 -> 2 -> 2 -> 3 -> 4
Explanation: For the given two linked list, 2, 2 and 4 are the elements in the intersection.

linkedlist_2
Try It Yourself
redirect icon

[Naive Approach] Search Each Node - O(n × m) Time and O(1) Auxiliary Space

The idea is to traverse the first linked list and search for each element in the second linked list. If a matching element is found, add it to the result linked list.

  • Traverse the first linked list.
  • For each node, search for its value in the second linked list.
  • Since both lists are sorted, stop the search when the current element in the second list becomes greater than the element being searched.
  • If the value is found, add it to the result linked list.
C++
#include <bits/stdc++.h>
using namespace std;

class Node {
  public:
    int data;
    Node* next;

    Node(int x) {
        data = x;
        next = nullptr;
    }
};

Node* findIntersection(Node* head1, Node* head2) {
    Node* head = nullptr;
    Node* tail = nullptr;

    // Traverse the first list
    while (head1 != nullptr) {
        Node* curr = head2;

        // Search for the current value in the second list
        while (curr != nullptr && curr->data < head1->data) {
            curr = curr->next;
        }

        // Add the value if it is present in both lists
        if (curr != nullptr && curr->data == head1->data) {
            Node* newNode = new Node(head1->data);

            if (head == nullptr) {
                head = newNode;
                tail = newNode;
            } else {
                tail->next = newNode;
                tail = newNode;
            }
        }

        head1 = head1->next;
    }

    return head;
}

void printList(Node* head) {
    while (head != nullptr) {
        cout << head->data;

        if (head->next != nullptr)
            cout << " -> ";

        head = head->next;
    }
    cout << endl;
}

int main() {
    
    // Create the first sorted linked list
    Node* head1 = new Node(1);
    head1->next = new Node(2);
    head1->next->next = new Node(3);
    head1->next->next->next = new Node(4);
    head1->next->next->next->next = new Node(6);

    // Create the second sorted linked list
    Node* head2 = new Node(2);
    head2->next = new Node(4);
    head2->next->next = new Node(6);
    head2->next->next->next = new Node(8);

    Node* result = findIntersection(head1, head2);

    printList(result);

    return 0;
}
Java
class Node {
    int data;
    Node next;

    Node(int x) {
        data = x;
        next = null;
    }
}

public class Main {

    static Node findIntersection(Node head1, Node head2) {
        Node head = null;
        Node tail = null;

        // Traverse the first list
        while (head1 != null) {
            Node curr = head2;

            // Search for the current value in the second list
            while (curr != null && curr.data < head1.data) {
                curr = curr.next;
            }

            // Add the value if it is present in both lists
            if (curr != null && curr.data == head1.data) {
                Node newNode = new Node(head1.data);

                if (head == null) {
                    head = newNode;
                    tail = newNode;
                } else {
                    tail.next = newNode;
                    tail = newNode;
                }
            }

            head1 = head1.next;
        }

        return head;
    }

    static void printList(Node head) {
        while (head != null) {
            System.out.print(head.data);

            if (head.next != null)
                System.out.print(" -> ");

            head = head.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {

        // Create the first sorted linked list
        Node head1 = new Node(1);
        head1.next = new Node(2);
        head1.next.next = new Node(3);
        head1.next.next.next = new Node(4);
        head1.next.next.next.next = new Node(6);

        // Create the second sorted linked list
        Node head2 = new Node(2);
        head2.next = new Node(4);
        head2.next.next = new Node(6);
        head2.next.next.next = new Node(8);

        Node result = findIntersection(head1, head2);

        printList(result);
    }
}
Python
class Node:
    def __init__(self, x):
        self.data = x
        self.next = None


def findIntersection(head1, head2):
    head = None
    tail = None

    # Traverse the first list
    while head1 is not None:
        curr = head2

        # Search for the current value in the second list
        while curr is not None and curr.data < head1.data:
            curr = curr.next

        # Add the value if it is present in both lists
        if curr is not None and curr.data == head1.data:
            newNode = Node(head1.data)

            if head is None:
                head = newNode
                tail = newNode
            else:
                tail.next = newNode
                tail = newNode

        head1 = head1.next

    return head


def printList(head):
    while head is not None:
        print(head.data, end="")

        if head.next is not None:
            print(" -> ", end="")

        head = head.next

    print()


if __name__ == "__main__":
    
    # Create the first sorted linked list
    head1 = Node(1)
    head1.next = Node(2)
    head1.next.next = Node(3)
    head1.next.next.next = Node(4)
    head1.next.next.next.next = Node(6)

    # Create the second sorted linked list
    head2 = Node(2)
    head2.next = Node(4)
    head2.next.next = Node(6)
    head2.next.next.next = Node(8)

    result = findIntersection(head1, head2)

    printList(result)
C#
using System;

class Node {
    public int data;
    public Node next;

    public Node(int x) {
        data = x;
        next = null;
    }
}

class Program {

    static Node findIntersection(Node head1, Node head2) {
        Node head = null;
        Node tail = null;

        // Traverse the first list
        while (head1 != null) {
            Node curr = head2;

            // Search for the current value in the second list
            while (curr != null && curr.data < head1.data) {
                curr = curr.next;
            }

            // Add the value if it is present in both lists
            if (curr != null && curr.data == head1.data) {
                Node newNode = new Node(head1.data);

                if (head == null) {
                    head = newNode;
                    tail = newNode;
                } else {
                    tail.next = newNode;
                    tail = newNode;
                }
            }

            head1 = head1.next;
        }

        return head;
    }

    static void printList(Node head) {
        while (head != null) {
            Console.Write(head.data);

            if (head.next != null)
                Console.Write(" -> ");

            head = head.next;
        }

        Console.WriteLine();
    }

    static void Main() {

        // Create the first sorted linked list
        Node head1 = new Node(1);
        head1.next = new Node(2);
        head1.next.next = new Node(3);
        head1.next.next.next = new Node(4);
        head1.next.next.next.next = new Node(6);

        // Create the second sorted linked list
        Node head2 = new Node(2);
        head2.next = new Node(4);
        head2.next.next = new Node(6);
        head2.next.next.next = new Node(8);

        Node result = findIntersection(head1, head2);

        printList(result);
    }
}
JavaScript
class Node {
    constructor(x) {
        this.data = x;
        this.next = null;
    }
}

function findIntersection(head1, head2) {
    let head = null;
    let tail = null;

    // Traverse the first list
    while (head1 !== null) {
        let curr = head2;

        // Search for the current value in the second list
        while (curr !== null && curr.data < head1.data) {
            curr = curr.next;
        }

        // Add the value if it is present in both lists
        if (curr !== null && curr.data === head1.data) {
            let newNode = new Node(head1.data);

            if (head === null) {
                head = newNode;
                tail = newNode;
            } else {
                tail.next = newNode;
                tail = newNode;
            }
        }

        head1 = head1.next;
    }

    return head;
}

function printList(head) {
    while (head !== null) {
        process.stdout.write(head.data.toString());

        if (head.next !== null)
            process.stdout.write(" -> ");

        head = head.next;
    }

    console.log();
}

// Create the first sorted linked list
let head1 = new Node(1);
head1.next = new Node(2);
head1.next.next = new Node(3);
head1.next.next.next = new Node(4);
head1.next.next.next.next = new Node(6);

// Create the second sorted linked list
let head2 = new Node(2);
head2.next = new Node(4);
head2.next.next = new Node(6);
head2.next.next.next = new Node(8);

let result = findIntersection(head1, head2);

printList(result);

Output
2 -> 4 -> 6

[Better Approach] Using Hashing - O(n + m) Time and O(n) Auxiliary Space

The idea is to store the frequency of each element in the first linked list using a hash map. Then, traverse the second list and add an element to the result if its frequency is greater than 0 in the Hash Map.

  • Store the frequency of each element from the first list.
  • Traverse the second list.
  • If the current element has a remaining frequency, add it to the result and decrease its frequency.
C++
#include <bits/stdc++.h>
using namespace std;

class Node {
  public:
    int data;
    Node* next;

    Node(int x) {
        data = x;
        next = nullptr;
    }
};

Node* findIntersection(Node* head1, Node* head2) {
    unordered_map<int, int> freq;

    // Store the frequency of each element in the first list
    while (head1 != nullptr) {
        freq[head1->data]++;
        head1 = head1->next;
    }

    Node* head = nullptr;
    Node* tail = nullptr;

    // Traverse the second list
    while (head2 != nullptr) {
        
        // Add the value if it has a remaining occurrence
        if (freq[head2->data] > 0) {
            Node* newNode = new Node(head2->data);
            freq[head2->data]--;

            if (head == nullptr) {
                head = newNode;
                tail = newNode;
            } else {
                tail->next = newNode;
                tail = newNode;
            }
        }

        head2 = head2->next;
    }

    return head;
}

void printList(Node* head) {
    while (head != nullptr) {
        cout << head->data;

        if (head->next != nullptr)
            cout << " -> ";

        head = head->next;
    }
    cout << endl;
}

int main() {

    // Create the first sorted linked list
    Node* head1 = new Node(1);
    head1->next = new Node(2);
    head1->next->next = new Node(2);
    head1->next->next->next = new Node(3);
    head1->next->next->next->next = new Node(4);

    // Create the second sorted linked list
    Node* head2 = new Node(2);
    head2->next = new Node(2);
    head2->next->next = new Node(2);
    head2->next->next->next = new Node(4);
    head2->next->next->next->next = new Node(5);

    Node* result = findIntersection(head1, head2);

    printList(result);

    return 0;
}
Java
import java.util.HashMap;

class Node {
    int data;
    Node next;

    Node(int x) {
        data = x;
        next = null;
    }
}

public class Main {

    static Node findIntersection(Node head1, Node head2) {
        HashMap<Integer, Integer> freq = new HashMap<>();

        // Store the frequency of each element in the first list
        while (head1 != null) {
            freq.put(head1.data, freq.getOrDefault(head1.data, 0) + 1);
            head1 = head1.next;
        }

        Node head = null;
        Node tail = null;

        // Traverse the second list
        while (head2 != null) {

            // Add the value if it has a remaining occurrence
            if (freq.getOrDefault(head2.data, 0) > 0) {
                Node newNode = new Node(head2.data);
                freq.put(head2.data, freq.get(head2.data) - 1);

                if (head == null) {
                    head = newNode;
                    tail = newNode;
                } else {
                    tail.next = newNode;
                    tail = newNode;
                }
            }

            head2 = head2.next;
        }

        return head;
    }

    static void printList(Node head) {
        while (head != null) {
            System.out.print(head.data);

            if (head.next != null)
                System.out.print(" -> ");

            head = head.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {

        // Create the first sorted linked list
        Node head1 = new Node(1);
        head1.next = new Node(2);
        head1.next.next = new Node(2);
        head1.next.next.next = new Node(3);
        head1.next.next.next.next = new Node(4);

        // Create the second sorted linked list
        Node head2 = new Node(2);
        head2.next = new Node(2);
        head2.next.next = new Node(2);
        head2.next.next.next = new Node(4);
        head2.next.next.next.next = new Node(5);

        Node result = findIntersection(head1, head2);

        printList(result);
    }
}
Python
class Node:
    def __init__(self, x):
        self.data = x
        self.next = None


def findIntersection(head1, head2):
    freq = {}

    # Store the frequency of each element in the first list
    while head1 is not None:
        freq[head1.data] = freq.get(head1.data, 0) + 1
        head1 = head1.next

    head = None
    tail = None

    # Traverse the second list
    while head2 is not None:

        # Add the value if it has a remaining occurrence
        if freq.get(head2.data, 0) > 0:
            newNode = Node(head2.data)
            freq[head2.data] -= 1

            if head is None:
                head = newNode
                tail = newNode
            else:
                tail.next = newNode
                tail = newNode

        head2 = head2.next

    return head


def printList(head):
    while head is not None:
        print(head.data, end="")

        if head.next is not None:
            print(" -> ", end="")

        head = head.next

    print()


def main():

    # Create the first sorted linked list
    head1 = Node(1)
    head1.next = Node(2)
    head1.next.next = Node(2)
    head1.next.next.next = Node(3)
    head1.next.next.next.next = Node(4)

    # Create the second sorted linked list
    head2 = Node(2)
    head2.next = Node(2)
    head2.next.next = Node(2)
    head2.next.next.next = Node(4)
    head2.next.next.next.next = Node(5)

    result = findIntersection(head1, head2)

    printList(result)


if __name__ == "__main__":
    main()
C#
using System;
using System.Collections.Generic;

class Node {
    public int data;
    public Node next;

    public Node(int x) {
        data = x;
        next = null;
    }
}

class Program {

    static Node findIntersection(Node head1, Node head2) {
        Dictionary<int, int> freq = new Dictionary<int, int>();

        // Store the frequency of each element in the first list
        while (head1 != null) {
            if (freq.ContainsKey(head1.data))
                freq[head1.data]++;
            else
                freq[head1.data] = 1;

            head1 = head1.next;
        }

        Node head = null;
        Node tail = null;

        // Traverse the second list
        while (head2 != null) {

            // Add the value if it has a remaining occurrence
            if (freq.ContainsKey(head2.data) && freq[head2.data] > 0) {
                Node newNode = new Node(head2.data);
                freq[head2.data]--;

                if (head == null) {
                    head = newNode;
                    tail = newNode;
                } else {
                    tail.next = newNode;
                    tail = newNode;
                }
            }

            head2 = head2.next;
        }

        return head;
    }

    static void printList(Node head) {
        while (head != null) {
            Console.Write(head.data);

            if (head.next != null)
                Console.Write(" -> ");

            head = head.next;
        }

        Console.WriteLine();
    }

    static void Main() {

        // Create the first sorted linked list
        Node head1 = new Node(1);
        head1.next = new Node(2);
        head1.next.next = new Node(2);
        head1.next.next.next = new Node(3);
        head1.next.next.next.next = new Node(4);

        // Create the second sorted linked list
        Node head2 = new Node(2);
        head2.next = new Node(2);
        head2.next.next = new Node(2);
        head2.next.next.next = new Node(4);
        head2.next.next.next.next = new Node(5);

        Node result = findIntersection(head1, head2);

        printList(result);
    }
}
JavaScript
class Node {
    constructor(x) {
        this.data = x;
        this.next = null;
    }
}

function findIntersection(head1, head2) {
    let freq = new Map();

    // Store the frequency of each element in the first list
    while (head1 !== null) {
        freq.set(head1.data, (freq.get(head1.data) || 0) + 1);
        head1 = head1.next;
    }

    let head = null;
    let tail = null;

    // Traverse the second list
    while (head2 !== null) {

        // Add the value if it has a remaining occurrence
        if ((freq.get(head2.data) || 0) > 0) {
            let newNode = new Node(head2.data);
            freq.set(head2.data, freq.get(head2.data) - 1);

            if (head === null) {
                head = newNode;
                tail = newNode;
            } else {
                tail.next = newNode;
                tail = newNode;
            }
        }

        head2 = head2.next;
    }

    return head;
}

function printList(head) {
    while (head !== null) {
        process.stdout.write(head.data.toString());

        if (head.next !== null)
            process.stdout.write(" -> ");

        head = head.next;
    }

    console.log();
}

function main() {

    // Create the first sorted linked list
    let head1 = new Node(1);
    head1.next = new Node(2);
    head1.next.next = new Node(2);
    head1.next.next.next = new Node(3);
    head1.next.next.next.next = new Node(4);

    // Create the second sorted linked list
    let head2 = new Node(2);
    head2.next = new Node(2);
    head2.next.next = new Node(2);
    head2.next.next.next = new Node(4);
    head2.next.next.next.next = new Node(5);

    let result = findIntersection(head1, head2);

    printList(result);
}

main();

Output
2 -> 2 -> 4

[Expected Approach] Using Two Pointer - O(n + m) Time and O(1) Auxiliary Space

Since both linked lists are sorted, we find the common elements by traversing both simultaneously.

The idea is to maintain two pointers, one for each list. At each step, compare their values:

  • If both values are equal, add the value to the result list and move both pointers.
  • If the value in the first list is smaller, move the pointer of the first list.
  • Otherwise, move the pointer of the second list.
  • Continue until either pointer reaches the end of its list.

This works because the lists are sorted. When one value is smaller, it cannot match the current or any previous value of the other list, so we can safely move that pointer.

C++
#include <bits/stdc++.h>
using namespace std;

class Node {
  public:
    int data;
    Node* next;

    Node(int x) {
        data = x;
        next = nullptr;
    }
};

Node* findIntersection(Node* head1, Node* head2) {
    Node* head = nullptr;
    Node* tail = nullptr;

    // Traverse both lists
    while (head1 != nullptr && head2 != nullptr) {

        // Add the value if it is present in both lists
        if (head1->data == head2->data) {
            Node* newNode = new Node(head1->data);

            if (head == nullptr) {
                head = newNode;
                tail = newNode;
            } else {
                tail->next = newNode;
                tail = newNode;
            }

            head1 = head1->next;
            head2 = head2->next;
        }
        else if (head1->data < head2->data) {
            head1 = head1->next;
        }
        else {
            head2 = head2->next;
        }
    }

    return head;
}

void printList(Node* head) {
    while (head != nullptr) {
        cout << head->data;

        if (head->next != nullptr)
            cout << " -> ";

        head = head->next;
    }
    cout << endl;
}

int main() {

    // Create the first sorted linked list
    Node* head1 = new Node(1);
    head1->next = new Node(2);
    head1->next->next = new Node(2);
    head1->next->next->next = new Node(3);
    head1->next->next->next->next = new Node(4);

    // Create the second sorted linked list
    Node* head2 = new Node(2);
    head2->next = new Node(2);
    head2->next->next = new Node(2);
    head2->next->next->next = new Node(4);
    head2->next->next->next->next = new Node(5);

    Node* result = findIntersection(head1, head2);

    printList(result);

    return 0;
}
Java
class Node {
    int data;
    Node next;

    Node(int x) {
        data = x;
        next = null;
    }
}

public class Main {

    static Node findIntersection(Node head1, Node head2) {
        Node head = null;
        Node tail = null;

        // Traverse both lists
        while (head1 != null && head2 != null) {

            // Add the value if it is present in both lists
            if (head1.data == head2.data) {
                Node newNode = new Node(head1.data);

                if (head == null) {
                    head = newNode;
                    tail = newNode;
                } else {
                    tail.next = newNode;
                    tail = newNode;
                }

                head1 = head1.next;
                head2 = head2.next;
            }
            else if (head1.data < head2.data) {
                head1 = head1.next;
            }
            else {
                head2 = head2.next;
            }
        }

        return head;
    }

    static void printList(Node head) {
        while (head != null) {
            System.out.print(head.data);

            if (head.next != null)
                System.out.print(" -> ");

            head = head.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {

        // Create the first sorted linked list
        Node head1 = new Node(1);
        head1.next = new Node(2);
        head1.next.next = new Node(2);
        head1.next.next.next = new Node(3);
        head1.next.next.next.next = new Node(4);

        // Create the second sorted linked list
        Node head2 = new Node(2);
        head2.next = new Node(2);
        head2.next.next = new Node(2);
        head2.next.next.next = new Node(4);
        head2.next.next.next.next = new Node(5);

        Node result = findIntersection(head1, head2);

        printList(result);
    }
}
Python
class Node:
    def __init__(self, x):
        self.data = x
        self.next = None


def findIntersection(head1, head2):
    head = None
    tail = None

    # Traverse both lists
    while head1 is not None and head2 is not None:

        # Add the value if it is present in both lists
        if head1.data == head2.data:
            newNode = Node(head1.data)

            if head is None:
                head = newNode
                tail = newNode
            else:
                tail.next = newNode
                tail = newNode

            head1 = head1.next
            head2 = head2.next

        elif head1.data < head2.data:
            head1 = head1.next

        else:
            head2 = head2.next

    return head


def printList(head):
    while head is not None:
        print(head.data, end="")

        if head.next is not None:
            print(" -> ", end="")

        head = head.next

    print()


def main():

    # Create the first sorted linked list
    head1 = Node(1)
    head1.next = Node(2)
    head1.next.next = Node(2)
    head1.next.next.next = Node(3)
    head1.next.next.next.next = Node(4)

    # Create the second sorted linked list
    head2 = Node(2)
    head2.next = Node(2)
    head2.next.next = Node(2)
    head2.next.next.next = Node(4)
    head2.next.next.next.next = Node(5)

    result = findIntersection(head1, head2)

    printList(result)


if __name__ == "__main__":
    main()
C#
using System;

class Node {
    public int data;
    public Node next;

    public Node(int x) {
        data = x;
        next = null;
    }
}

class Program {

    static Node findIntersection(Node head1, Node head2) {
        Node head = null;
        Node tail = null;

        // Traverse both lists
        while (head1 != null && head2 != null) {

            // Add the value if it is present in both lists
            if (head1.data == head2.data) {
                Node newNode = new Node(head1.data);

                if (head == null) {
                    head = newNode;
                    tail = newNode;
                } else {
                    tail.next = newNode;
                    tail = newNode;
                }

                head1 = head1.next;
                head2 = head2.next;
            }
            else if (head1.data < head2.data) {
                head1 = head1.next;
            }
            else {
                head2 = head2.next;
            }
        }

        return head;
    }

    static void printList(Node head) {
        while (head != null) {
            Console.Write(head.data);

            if (head.next != null)
                Console.Write(" -> ");

            head = head.next;
        }

        Console.WriteLine();
    }

    static void Main() {

        // Create the first sorted linked list
        Node head1 = new Node(1);
        head1.next = new Node(2);
        head1.next.next = new Node(2);
        head1.next.next.next = new Node(3);
        head1.next.next.next.next = new Node(4);

        // Create the second sorted linked list
        Node head2 = new Node(2);
        head2.next = new Node(2);
        head2.next.next = new Node(2);
        head2.next.next.next = new Node(4);
        head2.next.next.next.next = new Node(5);

        Node result = findIntersection(head1, head2);

        printList(result);
    }
}
JavaScript
class Node {
    constructor(x) {
        this.data = x;
        this.next = null;
    }
}

function findIntersection(head1, head2) {
    let head = null;
    let tail = null;

    // Traverse both lists
    while (head1 !== null && head2 !== null) {

        // Add the value if it is present in both lists
        if (head1.data === head2.data) {
            let newNode = new Node(head1.data);

            if (head === null) {
                head = newNode;
                tail = newNode;
            } else {
                tail.next = newNode;
                tail = newNode;
            }

            head1 = head1.next;
            head2 = head2.next;
        }
        else if (head1.data < head2.data) {
            head1 = head1.next;
        }
        else {
            head2 = head2.next;
        }
    }

    return head;
}

function printList(head) {
    while (head !== null) {
        process.stdout.write(head.data.toString());

        if (head.next !== null)
            process.stdout.write(" -> ");

        head = head.next;
    }

    console.log();
}

function main() {

    // Create the first sorted linked list
    let head1 = new Node(1);
    head1.next = new Node(2);
    head1.next.next = new Node(2);
    head1.next.next.next = new Node(3);
    head1.next.next.next.next = new Node(4);

    // Create the second sorted linked list
    let head2 = new Node(2);
    head2.next = new Node(2);
    head2.next.next = new Node(2);
    head2.next.next.next = new Node(4);
    head2.next.next.next.next = new Node(5);

    let result = findIntersection(head1, head2);

    printList(result);
}

main();

Output
2 -> 2 -> 4
Comment