五个解决办法教你C++中检测链表中的循环

给定一个链表,检查链表是否有循环。下图显示了带有循环的链表。

专注于为中小企业提供网站建设、网站制作服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业宁德免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了千余家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

以下是执行此操作的不同方法 

解决方案1:散列方法:

遍历该列表,并将节点地址始终放在哈希表中。在任何时候,如果达到NULL,则返回false,如果当前节点的下一个指向Hash中先前存储的任何节点,则返回true。

 
 
 
  1. #include  
  2. using namespace std; 
  3. struct Node { 
  4.     int data; 
  5.     struct Node* next; 
  6. }; 
  7.   
  8. void push(struct Node** head_ref, int new_data) 
  9.     struct Node* new_node = new Node; 
  10.     new_node->data = new_data; 
  11.     new_node->next = (*head_ref); 
  12.     (*head_ref) = new_node; 
  13. bool detectLoop(struct Node* h) 
  14.     unordered_set s; 
  15.     while (h != NULL) { 
  16.         if (s.find(h) != s.end()) 
  17.             return true; 
  18.         s.insert(h); 
  19.   
  20.         h = h->next; 
  21.     } 
  22.   
  23.     return false; 
  24. int main() 
  25.     struct Node* head = NULL; 
  26.   
  27.     push(&head, 20); 
  28.     push(&head, 4); 
  29.     push(&head, 15); 
  30.     push(&head, 10); 
  31.     head->next->next->next->next = head; 
  32.   
  33.     if (detectLoop(head)) 
  34.         cout << "Loop found"; 
  35.     else 
  36.         cout << "No Loop"; 
  37.   
  38.     return 0; 

复杂度分析:

时间复杂度: O(n)。
只需循环一次即可。

辅助空间: O(n)。
n是将值存储在哈希图中所需的空间。

解决方案2:通过修改链表数据结构,无需哈希图即可解决此问题。
方法:此解决方案需要修改基本链表数据结构。

  • 每个节点都有一个访问标志。
  • 遍历链接列表并继续标记访问的节点。
  • 如果您再次看到一个访问过的节点,那么就会有一个循环。该解决方案适用于O(n),但每个节点都需要其他信息。
  • 此解决方案的一种变体不需要修改基本数据结构,可以使用哈希来实现,只需将访问的节点的地址存储在哈希中,如果您看到哈希中已经存在的地址,则存在一个循环。

C++:

 
 
 
  1. #include  
  2. using namespace std; 
  3. struct Node { 
  4.     int data; 
  5.     struct Node* next; 
  6.     int flag; 
  7. }; 
  8.   
  9. void push(struct Node** head_ref, int new_data) 
  10.     struct Node* new_node = new Node; 
  11.     new_node->data = new_data; 
  12.   
  13.     new_node->flag = 0; 
  14.     new_node->next = (*head_ref); 
  15.     (*head_ref) = new_node; 
  16. bool detectLoop(struct Node* h) 
  17.     while (h != NULL) { 
  18.         if (h->flag == 1) 
  19.             return true; 
  20.         h->flag = 1; 
  21.   
  22.         h = h->next; 
  23.     } 
  24.   
  25.     return false; 
  26. int main() 
  27.     struct Node* head = NULL; 
  28.   
  29.     push(&head, 20); 
  30.     push(&head, 4); 
  31.     push(&head, 15); 
  32.     push(&head, 10); 
  33.     head->next->next->next->next = head; 
  34.   
  35.     if (detectLoop(head)) 
  36.         cout << "Loop found"; 
  37.     else 
  38.         cout << "No Loop"; 
  39.   
  40.     return 0; 

复杂度分析:

时间复杂度: O(n)。
只需循环一次即可。

辅助空间: O(1)。
不需要额外的空间。

解决方案3:Floyd的循环查找算法
方法:这是最快的方法,下面进行了介绍:

  • 使用两个指针遍历链表。
  • 将一个指针(slow_p)移动一个,将另一个指针(fast_p)移动两个。
  • 如果这些指针在同一节点相遇,则存在循环。如果指针不符合要求,则链接列表没有循环。

Floyd的循环查找算法的实现:

 
 
 
  1. #include  
  2. using namespace std; 
  3. class Node { 
  4. public: 
  5.     int data; 
  6.     Node* next; 
  7. }; 
  8.   
  9. void push(Node** head_ref, int new_data) 
  10.     Node* new_node = new Node(); 
  11.     new_node->data = new_data; 
  12.     new_node->next = (*head_ref); 
  13.     (*head_ref) = new_node; 
  14.   
  15. int detectLoop(Node* list) 
  16.     Node *slow_p = list, *fast_p = list; 
  17.   
  18.     while (slow_p && fast_p && fast_p->next) { 
  19.         slow_p = slow_p->next; 
  20.         fast_p = fast_p->next->next; 
  21.         if (slow_p == fast_p) { 
  22.             return 1; 
  23.         } 
  24.     } 
  25.     return 0; 
  26. int main() 
  27.     Node* head = NULL; 
  28.   
  29.     push(&head, 20); 
  30.     push(&head, 4); 
  31.     push(&head, 15); 
  32.     push(&head, 10); 
  33.     head->next->next->next->next = head; 
  34.     if (detectLoop(head)) 
  35.         cout << "Loop found"; 
  36.     else 
  37.         cout << "No Loop"; 
  38.     return 0; 

解决方案4:在不修改链接列表数据结构的情况下标记访问的节点
在此方法中,将创建一个临时节点。使遍历的每个节点的下一个指针指向该临时节点。这样,我们将节点的下一个指针用作标志来指示该节点是否已遍历。检查每个节点以查看下一个节点是否指向临时节点。在循环的第一个节点的情况下,第二次遍历该条件将成立,因此我们发现该循环存在。如果遇到一个指向null的节点,则循环不存在。
下面是上述方法的实现:

 
 
 
  1. #include  
  2. using namespace std; 
  3.   
  4. struct Node { 
  5.     int key; 
  6.     struct Node* next; 
  7. }; 
  8.   
  9. Node* newNode(int key) 
  10.     Node* temp = new Node; 
  11.     temp->key = key; 
  12.     temp->next = NULL; 
  13.     return temp; 
  14. void printList(Node* head) 
  15.     while (head != NULL) { 
  16.         cout << head->key << " "; 
  17.         head = head->next; 
  18.     } 
  19.     cout << endl; 
  20. bool detectLoop(Node* head) 
  21.     Node* temp = new Node; 
  22.     while (head != NULL) { 
  23.         if (head->next == NULL) { 
  24.             return false; 
  25.         } 
  26.         if (head->next == temp) { 
  27.             return true; 
  28.         } 
  29.         Node* nex = head->next; 
  30.         head->next = temp; 
  31.         head = nex; 
  32.     } 
  33.   
  34.     return false; 
  35. int main() 
  36.     Node* head = newNode(1); 
  37.     head->next = newNode(2); 
  38.     head->next->next = newNode(3); 
  39.     head->next->next->next = newNode(4); 
  40.     head->next->next->next->next = newNode(5); 
  41.     head->next->next->next->next->next = head->next->next; 
  42.   
  43.     bool found = detectLoop(head); 
  44.     if (found) 
  45.         cout << "Loop Found"; 
  46.     else 
  47.         cout << "No Loop"; 
  48.   
  49.     return 0; 

复杂度分析:

时间复杂度: O(n)。
只需循环一次即可。

辅助空间: O(1)。
不需要空间。

解决方案5:存放长度

在此方法中,将创建两个指针,第一个(始终指向头)和最后一个指针。每次最后一个指针移动时,我们都会计算第一个和最后一个之间的节点数,并检查当前节点数是否大于先前的节点数,如果是,我们通过移动最后一个指针进行操作,否则就意味着我们已经到达循环的终点,因此我们相应地返回输出。

 
 
 
  1. #include  
  2. using namespace std; 
  3.   
  4. struct Node { 
  5.     int key; 
  6.     struct Node* next; 
  7. }; 
  8.   
  9. Node* newNode(int key) 
  10.     Node* temp = new Node; 
  11.     temp->key = key; 
  12.     temp->next = NULL; 
  13.     return temp; 
  14. void printList(Node* head) 
  15.     while (head != NULL) { 
  16.         cout << head->key << " "; 
  17.         head = head->next; 
  18.     } 
  19.     cout << endl; 
  20. int distance(Node* first, Node* last) 
  21.     int counter = 0; 
  22.   
  23.     Node* curr; 
  24.     curr = first; 
  25.   
  26.     while (curr != last) { 
  27.         counter += 1; 
  28.         curr = curr->next; 
  29.     } 
  30.   
  31.     return counter + 1; 
  32. bool detectLoop(Node* head) 
  33.     Node* temp = new Node; 
  34.   
  35.     Node *first, *last; 
  36.     first = head; 
  37.     last = head; 
  38.     int current_length = 0; 
  39.     int prev_length = -1; 
  40.   
  41.     while (current_length > prev_length && last != NULL) { 
  42.           prev_length = current_length; 
  43.         current_length = distance(first, last); 
  44.         last = last->next; 
  45.     } 
  46.       
  47.     if (last == NULL) { 
  48.         return false; 
  49.     } 
  50.     else {  
  51.         return true; 
  52.     } 
  53. int main() 
  54.     Node* head = newNode(1); 
  55.     head->next = newNode(2); 
  56.     head->next->next = newNode(3); 
  57.     head->next->next->next = newNode(4); 
  58.     head->next->next->next->next = newNode(5); 
  59.     head->next->next->next->next->next = head->next->next; 
  60.   
  61.     bool found = detectLoop(head); 
  62.     if (found) 
  63.         cout << "Loop Found"; 
  64.     else 
  65.         cout << "No Loop Found"; 
  66.   
  67.     return 0; 

}

文章名称:五个解决办法教你C++中检测链表中的循环
路径分享:http://www.hantingmc.com/qtweb/news7/29907.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联