Intersection of Two Linked Lists Solution Leet code
import java.util.*;
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
HashSet<ListNode> n=new HashSet<ListNode>();
ListNode node=headA;
while(node!=null)
{
n.add(node);
node=node.next;
}
node=headB;
while(node!=null)
{
if(n.contains(node))
{
return node;
}
node=node.next;
}
return null;
}
}
Comments
Post a Comment