Intersection of two linked lists

Table of Contents

If you want to practice data structure and algorithm programs, you can go through 100+ java coding interview questions.

In this post, we will see how to find Intersection of two linked lists.


Problem

Given two singly linked lists, find if two linked lists intersect. If they intersect, find intersection point.


Solution

Here is simple algorithm to find Intersection of two linked lists.

  • Find the length of both singly linked lists.
  • Find the bigger linked list and iterate up to the difference between two linked list.
  • Please note that two linked list will become equal at this step.
  • Iterate over both linked list and check if there is any common node, if you find one, return it.
  • Else return null.

When you run above program, you will get below output

Intersecting node: 7

That;s all about finding Intersection of two linked lists.

Was this post helpful?

Leave a Reply

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