Skip to content

Instantly share code, notes, and snippets.

@ugandapinik
Created July 18, 2021 23:08
Show Gist options
  • Save ugandapinik/cc6399d13dbccfdfe85284b4cacc4508 to your computer and use it in GitHub Desktop.
Save ugandapinik/cc6399d13dbccfdfe85284b4cacc4508 to your computer and use it in GitHub Desktop.
static boolean hasCycle(SinglyLinkedListNode head) {
SinglyLinkedListNode first = head;
SinglyLinkedListNode second = head;
do{
first = first.next;
second = second.next;
second = second != null ? second.next : null;
}while (first != null && second != null && first != second);
if (first == second) return true;
else return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment