[Codewars] Can you get the loop ?

Can you get the loop ?

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int getLoopSize(Node* startNode)
{
Node *slow = startNode;
Node *fast = startNode;
do {
slow = slow->getNext();
fast = fast->getNext()->getNext();
} while(slow != fast);
int res = 0;
Node* runner = slow;
do {
runner = runner->getNext();
res += 1;
}while(runner != slow);
return res;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/06/08/PS/Codewars/can-you-get-the-loop/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.