Find the Intersection Point of Two Linked Lists in Java

A Linked List is a linear data structure in which each node has two blocks such that one block contains the value or data of the node and the other block contains the address of the next field.

Let us assume that we have a linked list such that each node contains a random pointer which is pointing to the other nodes in the list. The task is to find the node at which two linked lists intersect each other. If they don’t intersect, then return NULL or empty as output.

For Example




Explanation: Since the given linked list intersects at the node with the value ‘2’, we will return the value ‘2’ as the output.





Explanation: Since there are no common points, we will return NULL in this case.

Approach to Solve this Problem

We have two linked lists with a common point where they intersect each other. To find the intersection point, we will traverse both the linked lists till we find that they are equally pointing to the same value. At some point, the pointer to the next node of the linked list will be the same. Thus we will return the value of that point.

  • Take two linked lists with data and pointer to the next node.
  • A function commonPoint(listnode*headA, listnode*headB) takes two pointers of linked list respectively and returns the value of the common or intersection point of the linked list.
  • An integer function that finds the length of the linked list will return the length of both linked lists from the head of the list.
  • Now create a pointer to the head of both lists and traverse the list which is greater in its length till (length of first list – length of second list).
  • Now traverse the list till we find the next pointer is equal.
  • Return the value of that particular node where both the lists intersect.


Live Demo

public class Solution {
   static listnode headA,
   static class listnode {
      int data;
      listnode next;
      listnode(int d) {
         data = d;
         next = null;
   int count(listnode head) {
      int c = 0;
      while (head != null) {
         head =;
      return c;
   int commonPoint(listnode headA, listnode headB) {
      listnode p1 = headA;
      listnode p2 = headB;
      int c1 = count(headA);
      int c2 = count(headB);
      if (c1 > c2) {
         for (int i = 0; i < c1 - c2; i++) {
            if (p1 == null) {
               return - 1;
            p1 =;
      if (c1 < c2) {
         for (int i = 0; i < c2 - c1; i++) {
            if (p2 == null) {
               return - 1;
            p2 =;
      while (p1 != null &amp;&amp; p2 != null) {
         if ( == {
         p1 =;
         p2 =;
      return - 1;
   public static void main(String[] args) {
      Solution list = new Solution();
      list.headA = new listnode(5); = new listnode(4); = new listnode(9); = new listnode(7); = new listnode(1);
      list.headB = new listnode(6); = new listnode(7); = new listnode(1);
      System.out.println(list.commonPoint(headA, headB));

Running the above code will generate the output as,



Explanation: The given linked lists are intersecting at 7.

Updated on: 23-Feb-2021


Kickstart Your Career

Get certified by completing the course

Get Started