Будет ли работать алгоритм обнаружения цикла Флойда с быстрым указателем, имеющим трехступенчатый прыжок?JAVA

Программисты JAVA общаются здесь
Ответить
Anonymous
 Будет ли работать алгоритм обнаружения цикла Флойда с быстрым указателем, имеющим трехступенчатый прыжок?

Сообщение Anonymous »

class SingleLinkedList {
Заголовок узла списка; // создание экземпляра возможно без создания объекта

Код: Выделить всё

class Listnode { // classes and methods can be static or non-static
int data;
Listnode next;
Listnode(int data) {
this.data = data;
this.next = null;
}
}
класс SingleLinkedList1 расширяет SingleLinkedList {

Код: Выделить всё

// Remove duplicates from a sorted linked list
void delete_duplicate() {
Listnode current = head;
while (current != null && current.next != null) {
if (current.data == current.next.data) {
current.next = current.next.next;
} else {
current = current.next;
}
}
}

// Insert a node in a sorted linked list
void insert_node_sorted_list(int x) {
Listnode key = new Listnode(x);
if (head == null || head.data >= key.data) {  // Handle empty list or head insertion
key.next = head;
head = key;
return;
}

Listnode current = head;
Listnode previous = null;
while (current != null && current.data < key.data) {
previous = current;
current = current.next;
}
previous.next = key;
key.next = current;
}

// Remove the first occurrence of a key from the list
void remove_key(int x) {
Listnode current = head;
Listnode previous = null;
if (head != null && head.data == x) {
head = head.next;
return;
}
while (current != null && current.data != x) {
previous = current;
current = current.next;
}
if (current != null) {
previous.next = current.next;
}
}

// Detect a loop in the linked list using Floyd's Cycle Detection Algorithm
boolean detect_loop() {
Listnode fast_ptr, slow_ptr;
slow_ptr = fast_ptr = head;
while (fast_ptr != null && fast_ptr.next != null) {
fast_ptr = fast_ptr.next.next;
slow_ptr = slow_ptr.next;
if (slow_ptr == fast_ptr) {
return true;
}
}
return false;
}

// Find the starting node of a loop in the linked list
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;
slow_ptr = slow_ptr.next;
if (fast_ptr == slow_ptr) {
return get_the_starting_node(slow_ptr);
}
}
return null;
}

// Helper function to find the starting node of a loop
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;
}

public static void main(String[] args) {
SingleLinkedList1 sll = new SingleLinkedList1();
sll.head = sll.new Listnode(1);
Listnode second = sll.new Listnode(2);
Listnode third = sll.new Listnode(3);
Listnode fourth = sll.new Listnode(3);
Listnode fifth = sll.new Listnode(5);
Listnode sixth = sll.new Listnode(5); // 1-->2-->3-->3-->5-->5

sll.head.next = second;
second.next = third;
third.next = fourth;
fourth.next = fifth;
fifth.next = sixth;
sixth.next = third; // Creating a loop

System.out.println(sll.detect_loop()); // Detect if there's a loop

Listnode startNode = sll.start_node_of_loop();
if (startNode != null) {
System.out.println("Start of loop: " + startNode.data); // Print the start of the loop
} else {
System.out.println("No loop detected.");
}
}
Будет ли работать алгоритм Флойда для обнаружения цикла, если быстрый указатель имеет три шага перехода вместо двух? может ли алгоритм Флойда работать, если у быстрых указателей есть скачок шагов 3,4,5,7 ..и т.д.?

Подробнее здесь: https://stackoverflow.com/questions/791 ... ng-three-s
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»