What is the time & space complexity of this method I wrote which returns the first node of the intersection between two singly linked lists (and if none found just null)?
public Node getFirstNodeWhichIntersects(Node node1, Node node2) {
Node currentPtr1 = node1;
Node currentPtr2 = node2;
Node firstNode = null;
while (currentPtr1 != null) {
currentPtr2 = node2;
while (currentPtr2 != null) {
if (currentPtr1 == currentPtr2) {
return currentPtr1;
}
currentPtr2 = currentPtr2.next;
}
currentPtr1 = currentPtr1.next;
}
return firstNode;
}