Created
April 14, 2017 07:17
-
-
Save anil477/fe005bf3d4a020e677669869c958eaf6 to your computer and use it in GitHub Desktop.
Intersection of two sorted linked list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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