Intersection of Two Linked Lists

This image is taken from Leetcode problem 160
  1. Create a common ListNode initialised to NULL. (we will store the commonNode here).
  2. Take two pointers i and j to traverse through lis1 and list2 respectively
  3. Initialise ListNode i = list1 and ListNode j = list 2
  1. Create a hashset of ListNodes
  2. Create a common node initialized to null
  3. First, traverse in list1 and add all the nodes addresses into the hashset
  4. Secondly, traverse in the list2 and check if any of these node was already a part of list1. If yes store that node in common. Break out and return that common node.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
kumari.Prakriti

kumari.Prakriti

It takes a dream to get started, desire to keep going and determination to finish.