What are the methods of detecting loops in linked lists in C++
This article mainly explains "what are the loop methods for detecting linked lists in C++". The explanation in this article is simple and clear and easy to learn and understand. let's study and learn "what are the loop methods for detecting linked lists in C++"?
Given a linked list, check whether the linked list has loops. The following figure shows a linked list with loops.
Here are different ways to do this
Solution 1: hash method:
Iterate through the list and always put the node address in the hash table. At any time, if NULL is reached, false is returned, and true is returned if the next node of the current node points to any node previously stored in Hash.
# include using namespace std; struct Node {int data; struct Node* next;}; void push (struct Node** head_ref, int new_data) {struct Node* new_node = new Node; new_node- > data = new_data; new_node- > next = (* head_ref); (* head_ref) = new_node;} bool detectLoop (struct Node* h) {unordered_set s While (h! = NULL) {if (s.find (h)! = s.end ()) return true; s.insert (h); h = h-> next;} return false;} int main () {struct Node* head = NULL; push (& head, 20); push (& head, 4); push (& head, 15); push (& head, 10) Head- > next- > next = head; if (detectLoop (head)) cout flag = 0; new_node- > next = (* head_ref); (* head_ref) = new_node;} bool detectLoop (struct Node* h) {while (h! = NULL) {if (h-> flag = = 1) return true; h-> flag = 1; h = h-> next } return false;} int main () {struct Node* head = NULL; push (& head, 20); push (& head, 4); push (& head, 15); push (& head, 10); head- > next- > next = head; if (detectLoop (head)) cout next = (* head_ref); (* head_ref) = new_node } int detectLoop (Node* list) {Node* slow_p = list, * fast_p = list; while (slow_p & & fast_p & & fast_p- > next) {slow_p = slow_p- > next; fast_p = fast_p- > next- > next; if (slow_p = = fast_p) {return 1;}} return 0 } int main () {Node* head = NULL; push (& head, 20); push (& head, 4); push (& head, 15); push (& head, 10); head- > next- > next = head; if (detectLoop (head)) cout next = NULL; return temp;} void printList (Node* head) {while (head! = NULL) {cout key next } cout next = = NULL) {return false;} if (head- > next = = temp) {return true;} Node* nex = head- > next; head- > next = temp; head = nex;} return false;} int main () {Node* head = newNode (1); head- > next = newNode (2) Head- > next- > next = newNode (3); head- > next- > next- > next = newNode (4); head- > next- > next = newNode (5); head- > next- > next = head- > next- > next; bool found = detectLoop (head); if (found) cout next = NULL; return temp;} void printList (Node* head) {while (head! = NULL) {cout key next } cout next;} return counter + 1;} bool detectLoop (Node* head) Node* temp = new Node; Node* first, * last; first = head; last = head; int current_length = 0; int prev_length =-1; while (current_length > prev_length & & last! = NULL) {prev_length = current_length; current_length = distance (first, last) Last = last- > next;} if (last = = NULL) {return false;} else {return true;}} int main () {Node* head = newNode (1); head- > next = newNode (2); head- > next- > next = newNode (3); head- > next- > next- > next = newNode (4); head- > next- > next = newNode (5) Head- > next- > next = head- > next- > next; bool found = detectLoop (head); if (found) cout