Skip to content

Instantly share code, notes, and snippets.

@anil477
Created April 14, 2017 07:17
Show Gist options
  • Save anil477/fe005bf3d4a020e677669869c958eaf6 to your computer and use it in GitHub Desktop.
Save anil477/fe005bf3d4a020e677669869c958eaf6 to your computer and use it in GitHub Desktop.
Intersection of two sorted linked list
private Node getIntersectedLinkedList(Node first, Node second){
Node ptr = first;
Node ptr1 = second;
Node previous = null;
Node firstIntersectedNode = null;
while (ptr != null) {
ptr1=second;
while (ptr1!= null && ptr.getValue() != ptr1.getValue() ) {
ptr1 = ptr1.getNext();
}
if(ptr1 !=null){
Node node = new Node();
if (firstIntersectedNode == null) {
firstIntersectedNode = node;
}
node.setValue(ptr.getValue());
if (previous != null) {
previous.setNext(node);
}
previous = node;
}
ptr = ptr.getNext();
}
return firstIntersectedNode;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment