Skip to content

Instantly share code, notes, and snippets.

@reuke
Created October 3, 2017 14:48
Show Gist options
  • Save reuke/5cfb56f95f89bfbe74ccdb0805f96961 to your computer and use it in GitHub Desktop.
Save reuke/5cfb56f95f89bfbe74ccdb0805f96961 to your computer and use it in GitHub Desktop.
public class Kata
{
public static int getLoopSize(LoopDetector.Node startNode)
{
LoopDetector.Node prev = null;
LoopDetector.Node curr = startNode;
LoopDetector.Node temp = null;
int n1 = 0;
int n2 = 0;
do
{
temp = curr;
curr = curr.next;
temp.next = prev;
prev = temp;
n1++;
} while (curr != null);
curr = startNode;
do
{
temp = curr;
curr = curr.next;
temp.next = null;
n2++;
} while (curr.next != null);
return 2 * n2 - n1 + 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment