自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

五個解決辦法教你C++中檢測鏈表中的循環(huán)

開發(fā) 后端
通過5個解決方案教你C++中檢測鏈表中的循環(huán),快來看看,是否對你有幫助!

五個解決辦法教你C++中檢測鏈表中的循環(huán)

給定一個鏈表,檢查鏈表是否有循環(huán)。下圖顯示了帶有循環(huán)的鏈表。

五個解決辦法教你C++中檢測鏈表中的循環(huán)

 

以下是執(zhí)行此操作的不同方法 

 

解決方案1:散列方法:

遍歷該列表,并將節(jié)點(diǎn)地址始終放在哈希表中。在任何時候,如果達(dá)到NULL,則返回false,如果當(dāng)前節(jié)點(diǎn)的下一個指向Hash中先前存儲的任何節(jié)點(diǎn),則返回true。

  1. #include <bits/stdc++.h> 
  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<Node*> 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; 

復(fù)雜度分析:

時間復(fù)雜度: O(n)。
只需循環(huán)一次即可。

輔助空間: O(n)。
n是將值存儲在哈希圖中所需的空間。

解決方案2:通過修改鏈表數(shù)據(jù)結(jié)構(gòu),無需哈希圖即可解決此問題。
方法:此解決方案需要修改基本鏈表數(shù)據(jù)結(jié)構(gòu)。

  • 每個節(jié)點(diǎn)都有一個訪問標(biāo)志。
  • 遍歷鏈接列表并繼續(xù)標(biāo)記訪問的節(jié)點(diǎn)。
  • 如果您再次看到一個訪問過的節(jié)點(diǎn),那么就會有一個循環(huán)。該解決方案適用于O(n),但每個節(jié)點(diǎn)都需要其他信息。
  • 此解決方案的一種變體不需要修改基本數(shù)據(jù)結(jié)構(gòu),可以使用哈希來實(shí)現(xiàn),只需將訪問的節(jié)點(diǎn)的地址存儲在哈希中,如果您看到哈希中已經(jīng)存在的地址,則存在一個循環(huán)。

C++:

  1. #include <bits/stdc++.h> 
  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; 

復(fù)雜度分析:

時間復(fù)雜度: O(n)。
只需循環(huán)一次即可。

輔助空間: O(1)。
不需要額外的空間。

解決方案3:Floyd的循環(huán)查找算法
方法:這是最快的方法,下面進(jìn)行了介紹:

  • 使用兩個指針遍歷鏈表。
  • 將一個指針(slow_p)移動一個,將另一個指針(fast_p)移動兩個。
  • 如果這些指針在同一節(jié)點(diǎn)相遇,則存在循環(huán)。如果指針不符合要求,則鏈接列表沒有循環(huán)。

Floyd的循環(huán)查找算法的實(shí)現(xiàn):

  1. #include <bits/stdc++.h> 
  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:在不修改鏈接列表數(shù)據(jù)結(jié)構(gòu)的情況下標(biāo)記訪問的節(jié)點(diǎn)
在此方法中,將創(chuàng)建一個臨時節(jié)點(diǎn)。使遍歷的每個節(jié)點(diǎn)的下一個指針指向該臨時節(jié)點(diǎn)。這樣,我們將節(jié)點(diǎn)的下一個指針用作標(biāo)志來指示該節(jié)點(diǎn)是否已遍歷。檢查每個節(jié)點(diǎn)以查看下一個節(jié)點(diǎn)是否指向臨時節(jié)點(diǎn)。在循環(huán)的第一個節(jié)點(diǎn)的情況下,第二次遍歷該條件將成立,因此我們發(fā)現(xiàn)該循環(huán)存在。如果遇到一個指向null的節(jié)點(diǎn),則循環(huán)不存在。
下面是上述方法的實(shí)現(xiàn):

  1. #include <bits/stdc++.h> 
  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; 

復(fù)雜度分析:

時間復(fù)雜度: O(n)。
只需循環(huán)一次即可。

輔助空間: O(1)。
不需要空間。

解決方案5:存放長度

在此方法中,將創(chuàng)建兩個指針,第一個(始終指向頭)和最后一個指針。每次最后一個指針移動時,我們都會計算第一個和最后一個之間的節(jié)點(diǎn)數(shù),并檢查當(dāng)前節(jié)點(diǎn)數(shù)是否大于先前的節(jié)點(diǎn)數(shù),如果是,我們通過移動最后一個指針進(jìn)行操作,否則就意味著我們已經(jīng)到達(dá)循環(huán)的終點(diǎn),因此我們相應(yīng)地返回輸出。

  1. #include <bits/stdc++.h> 
  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(firstlast); 
  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; 

} 

責(zé)任編輯:龐桂玉 來源: 今日頭條
相關(guān)推薦

2017-05-04 20:15:51

iOSNSTimer循環(huán)引用

2023-10-29 00:55:44

2012-03-14 10:58:27

Java

2011-06-16 09:28:02

C++內(nèi)存泄漏

2021-10-31 20:24:24

C++for循環(huán)

2024-10-07 08:26:05

編程Python異常處理

2015-06-10 13:49:53

2023-09-27 23:24:50

C++鏈表

2023-08-28 10:54:09

容器Docker

2009-09-28 10:09:09

Linux內(nèi)核Linux循環(huán)鏈表

2023-11-03 07:58:54

CORSSpring

2011-03-04 13:07:47

Filezilla

2010-01-21 09:34:57

C++語法

2010-01-27 16:05:06

C++堆棧

2009-06-03 16:41:21

Eclipse亂碼Eclipse

2011-07-27 19:05:35

2009-12-29 10:24:51

Linux內(nèi)核循環(huán)鏈表

2009-05-31 09:07:35

Oracle鎖定

2025-03-19 08:40:00

2011-01-19 17:54:48

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號