Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Intersection of Two Linked Lists Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Intersection of Two Linked List

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

Given two linked lists, find the node at which they intersect. If the two lists do not intersect, return null.

Naive Approach

The simple solution is to use two nested loop. The outer loop will be for each node of the first list and the inner loop to compare all the node of the second list with the current node of the first list. If both the nodes are equal at some point then return that node otherwise return null.

Analysis

  • Time Complexity: O(n * m)
  • Auxiliary Space Complexity: O(1)

Implementation

C++
/* This is the ListNode class definition

class ListNode {
public:
	int data;
	ListNode* next;

	ListNode(int data) {
		this->data = data;
		this->next = NULL;
	}
};
*/

ListNode* getIntersectionNode(ListNode *A, ListNode *B) {
    ListNode* firstCurrNode = A;
	while (firstCurrNode != NULL) {
		ListNode* secondCurrNode = B;
		while (secondCurrNode != NULL) {
			if (firstCurrNode == secondCurrNode) {
				return firstCurrNode;
			}
			secondCurrNode = secondCurrNode->next;
		}
		firstCurrNode = firstCurrNode->next;
	}
	return NULL;
}
Java
/** This is the ListNode class definition

class ListNode {
	int data;
	ListNode next;

	ListNode(int data) {
		this.data = data;
		this.next = null;
	}
}
**/
class Solution {
	ListNode getIntersectionNode(ListNode A, ListNode B) {
		ListNode firstCurrNode = A;
		while (firstCurrNode != null) {
			ListNode secondCurrNode = B;
			while (secondCurrNode != null) {
				if (firstCurrNode == secondCurrNode) {
					return firstCurrNode;
				}
				secondCurrNode = secondCurrNode.next;
			}
			firstCurrNode = firstCurrNode.next;
		}
		return null;
	}
}

Optimal Approach

Let's say the length of the two lists are length1 and length2 (length1 >= length2). Traverse the first list by a distance of length1 - length2 so that from here onwards both the list have an equal number of nodes. Now traverse both the lists simultaneously and compare the corresponding nodes. If two nodes are equal at some point then return that node otherwise return null.

Analysis

  • Time Complexity: O(n + m)
  • Auxiliary Space Complexity: O(1)

Implementation

C++
/* This is the ListNode class definition

class ListNode {
public:
	int data;
	ListNode* next;

	ListNode(int data) {
		this->data = data;
		this->next = NULL;
	}
};
*/
ListNode* findIntersectionNode (ListNode *A, ListNode *B, int difference) {
	while(difference > 0) {
		A = A->next;
		difference--;
	}
	while (A != NULL && B!= NULL) {
		if (A==B) {
			return A;
		}
		A = A->next;
		B = B->next;
	}
	return NULL;
}
ListNode* getIntersectionNode(ListNode *A, ListNode *B) {
    int firstLength = 0;
	ListNode* firstCurrNode = A;
	while (firstCurrNode != NULL) {
		firstLength++;
		firstCurrNode = firstCurrNode->next;
	}
	int secondLength = 0;
	ListNode* secondCurrNode = B;
	while (secondCurrNode != NULL) {
		secondLength++;
		secondCurrNode = secondCurrNode->next;
	}
	if (firstLength < secondLength) {
		return findIntersectionNode (B, A, secondLength - firstLength);
	} else {
		return findIntersectionNode (A, B, firstLength - secondLength);
	}
}
Java
/** This is the ListNode class definition

class ListNode {
	int data;
	ListNode next;

	ListNode(int data) {
		this.data = data;
		this.next = null;
	}
}
**/
class Solution {
	ListNode findIntersectionNode (ListNode A, ListNode B, int difference) {
		while(difference > 0) {
			A = A.next;
			difference--;
		}
		while (A != null && B!= null) {
			if (A==B) {
				return A;
			}
			A = A.next;
			B = B.next;
		}
		return null;
	}
	ListNode getIntersectionNode(ListNode A, ListNode B) {
		int firstLength = 0;
		ListNode firstCurrNode = A;
		while (firstCurrNode != null) {
			firstLength++;
			firstCurrNode = firstCurrNode.next;
		}
		int secondLength = 0;
		ListNode secondCurrNode = B;
		while (secondCurrNode != null) {
			secondLength++;
			secondCurrNode = secondCurrNode.next;
		}
		if (firstLength < secondLength) {
			return findIntersectionNode (B, A, secondLength - firstLength);
		} else {
			return findIntersectionNode (A, B, firstLength - secondLength);
		}
	}
}
Related Content
Add Element at Kth Position in Linked List
Add Two Numbers as Lists
Add One to Linked List
Append Linked Lists
Clone List with Random Pointer
Remove Element at Kth Position in Linked List
Delete Node From Linked List
Delete Xth Node From End of Linked List
Detect Loop in Linked List
Find xth Node from End of Linked List
Flatten a Multi-Level Linked List
Insertion Sort Linked List
Kth Element in Linked List
Linked List Palindrome
Linked List to Array
Merge Sort Linked List
Merge Two Sorted Linked List
Middle Element of Linked List
Partition List
Print Linked List
Print Reversed Linked List
Remove Duplicates from Sorted Linked List
Remove Duplicates from Sorted Linked List - II
Remove Loop From Linked List
Remove occurrences in Linked List
Reorder List
Reverse a Linked List
Reverse a Linked List II
Reverse a Linked List in k-groups
Rotate a Linked List
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet