Register for our webinar

How to Nail your next Technical Interview

1 hour
Loading...
1
Enter details
2
Select webinar slot
*Invalid Name
*Invalid Name
By sharing your contact details, you agree to our privacy policy.
Step 1
Step 2
Congratulations!
You have registered for our webinar
check-mark
Oops! Something went wrong while submitting the form.
1
Enter details
2
Select webinar slot
*All webinar slots are in the Asia/Kolkata timezone
Step 1
Step 2
check-mark
Confirmed
You are scheduled with Interview Kickstart.
Redirecting...
Oops! Something went wrong while submitting the form.
close-icon
Iks white logo

You may be missing out on a 66.5% salary hike*

Nick Camilleri

Head of Career Skills Development & Coaching
*Based on past data of successful IK students
Iks white logo
Help us know you better!

How many years of coding experience do you have?

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Iks white logo

FREE course on 'Sorting Algorithms' by Omkar Deshpande (Stanford PhD, Head of Curriculum, IK)

Thank you! Please check your inbox for the course details.
Oops! Something went wrong while submitting the form.
closeAbout usWhy usInstructorsReviewsCostFAQContactBlogRegister for Webinar
Our June 2021 cohorts are filling up quickly. Join our free webinar to Uplevel your career
close

Intersection of Two Linked Lists Problem

Problem Statement

Find Intersection Of Two Singly Linked Lists

Given two singly linked lists, find their intersection if any.

The intersection is the node where the two lists merge, the first node that belongs to both lists, node 4 in the following illustration:

Example

Input: Pointers to the head nodes of the linked list 1 and 2, respectively.

Output: 4

The lists intersect in node 4.

Notes

Input Parameters: Function has two arguments: L1 and L2, the pointers to heads of the singly linked lists.

Output: Function must return an integer, the value from the intersection node or -1 if no intersection exists.

Constraints:

● 0 <= values in the list nodes <= 10^9

● 0 <= number of nodes in a list <= 10^5

Let us first try to solve a simpler problem. What if the two linked lists are of the same length? We could start comparing from the first node of both lists. If the two nodes are the same (same address) then it is the intersection node else we advance both pointers to their next nodes and again compare and so on. If we reach the end of both lists, it means they do not intersect.

Now suppose one list is longer than the other by X nodes. It is easy to see that the intersection cannot be found among the first X nodes of the longer list. X is easy to calculate by measuring the length of each list. Then we can reduce the problem to the simpler one by skipping the first X nodes in the longer list. Using this approach we would walk through the lists two times total: once for measuring their lengths and once again for comparing the nodes.

Time Complexity:

O(N) where N is the length of the longer list (or lengths of the two lists combined).

Auxiliary Space Used:

O(1).

Space Complexity:

O(N) where N is the size of input.


// -------- START --------

    static int get_size(LinkedListNode head)
    {
        int N = 0;
        while (head != null)
        {
            N++;
            head = head.next;
        }
        return N;
    }

    static int find_intersection(LinkedListNode l1, LinkedListNode l2)
    {
        // Size of first linked list.
        int N1 = get_size(l1);                                      
        // Size of second linked list.
        int N2 = get_size(l2);                                      
        while (N1 > N2)                                     
        {
            l1 = l1.next;
            N1--;
        }
        while (N2 > N1)                                     
        {
            l2 = l2.next;
            N2--;
        }
        // Comparing address.
        while (l1 != null && l1 != l2)                              
        {
            l1 = l1.next;
            l2 = l2.next;
        }
        // If we have reached end.
        if (l1 == null)                                             
        {
            return -1;
        }
        // Intersection at node pointed by current value of l1.
        return l1.val;                                              
    }

    // -------- END --------

Try yourself in the Editor

Note: Input and Output will already be taken care of.

Intersection of Two Linked Lists Problem

Problem Statement

Find Intersection Of Two Singly Linked Lists

Given two singly linked lists, find their intersection if any.

The intersection is the node where the two lists merge, the first node that belongs to both lists, node 4 in the following illustration:

Example

Input: Pointers to the head nodes of the linked list 1 and 2, respectively.

Output: 4

The lists intersect in node 4.

Notes

Input Parameters: Function has two arguments: L1 and L2, the pointers to heads of the singly linked lists.

Output: Function must return an integer, the value from the intersection node or -1 if no intersection exists.

Constraints:

● 0 <= values in the list nodes <= 10^9

● 0 <= number of nodes in a list <= 10^5

Let us first try to solve a simpler problem. What if the two linked lists are of the same length? We could start comparing from the first node of both lists. If the two nodes are the same (same address) then it is the intersection node else we advance both pointers to their next nodes and again compare and so on. If we reach the end of both lists, it means they do not intersect.

Now suppose one list is longer than the other by X nodes. It is easy to see that the intersection cannot be found among the first X nodes of the longer list. X is easy to calculate by measuring the length of each list. Then we can reduce the problem to the simpler one by skipping the first X nodes in the longer list. Using this approach we would walk through the lists two times total: once for measuring their lengths and once again for comparing the nodes.

Time Complexity:

O(N) where N is the length of the longer list (or lengths of the two lists combined).

Auxiliary Space Used:

O(1).

Space Complexity:

O(N) where N is the size of input.


// -------- START --------

    static int get_size(LinkedListNode head)
    {
        int N = 0;
        while (head != null)
        {
            N++;
            head = head.next;
        }
        return N;
    }

    static int find_intersection(LinkedListNode l1, LinkedListNode l2)
    {
        // Size of first linked list.
        int N1 = get_size(l1);                                      
        // Size of second linked list.
        int N2 = get_size(l2);                                      
        while (N1 > N2)                                     
        {
            l1 = l1.next;
            N1--;
        }
        while (N2 > N1)                                     
        {
            l2 = l2.next;
            N2--;
        }
        // Comparing address.
        while (l1 != null && l1 != l2)                              
        {
            l1 = l1.next;
            l2 = l2.next;
        }
        // If we have reached end.
        if (l1 == null)                                             
        {
            return -1;
        }
        // Intersection at node pointed by current value of l1.
        return l1.val;                                              
    }

    // -------- END --------

Worried About Failing Tech Interviews?

Attend our free webinar to amp up your career and get the salary you deserve.

Ryan-image
Hosted By
Ryan Valles
Founder, Interview Kickstart
blue tick
Accelerate your Interview prep with Tier-1 tech instructors
blue tick
360° courses that have helped 14,000+ tech professionals
blue tick
100% money-back guarantee*
Register for Webinar
All Blog Posts