Intersection of two linked lists

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.

💻 Awesome Tech Resources:
  • Looking for ⚒️ tech jobs? Go to our job portal.
  • Looking for tech events? Go to tech events 🗓️ Calendar.️
  • 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.


import_contacts

You may also like:

Related Posts

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.