Код: Выделить всё
Listnode start_node_of_loop() {
Listnode fast_ptr = head;
Listnode slow_ptr = head;
while (fast_ptr != null && fast_ptr.next != null) {
fast_ptr = fast_ptr.next.next.next;
slow_ptr = slow_ptr.next;
if (fast_ptr == slow_ptr) {
return get_the_starting_node(slow_ptr);
}
}
return null;
}
Listnode get_the_starting_node(Listnode slow_ptr) {
Listnode temp = head;
while (slow_ptr != temp) {
temp = temp.next;
slow_ptr = slow_ptr.next;
}
return temp;
}
Подробнее здесь: https://stackoverflow.com/questions/791 ... -jump-work
Мобильная версия