Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Intersection of Two Linked Lists #1636

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 36 additions & 0 deletions Data-Structures/Linked-List/Intersection of Two Linked Lists
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* A LinkedList based solution for Intersection of Two Linked Lists
*
*/
import { Node } from './SinglyLinkedList.js';

/*
Problem Statement:
Given the heads of two singly linked-lists headA and headB, return the node at which the two lists intersect. If the two linked lists have no intersection at all, return null.

Link for the Problem: https://leetcode.com/problems/intersection-of-two-linked-lists/description/
*/

class getIntersectionNode{

solution(head1,head2){
let t1=head1; /*reference to list 1*/
let t2=head2; /*reference to list 2*/
if(t1==null||t2==null)
{
return null;
}
/* checking till we get same nodes in list1 and list2 */
while(t1!=t2){
t1=t1.next;
t2=t2.next;
if(t1==t2)
return t1;
if(t1==null)
t1=head2;
if(t2==null)
t2=head1;
}
return t1;
}
}