Skip to content

Instantly share code, notes, and snippets.

@linnykoleh
Created April 9, 2017 11:39
Show Gist options
  • Save linnykoleh/af529290f80a9ba9e6f6d0a71a912576 to your computer and use it in GitHub Desktop.
Save linnykoleh/af529290f80a9ba9e6f6d0a71a912576 to your computer and use it in GitHub Desktop.
private static ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if(headA == null || headB == null){
return null;
}
final Set<ListNode> nodesA = new HashSet<>();
ListNode currentA = headA;
ListNode currentB = headB;
while (currentA != null){
nodesA.add(currentA);
currentA = currentA.next;
}
while (currentB != null){
if(nodesA.contains(currentB)){
return currentB;
}
currentB = currentB.next;
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment