|
12 | 12 | *
|
13 | 13 | **********************************************************************************/
|
14 | 14 |
|
15 |
| -/* |
16 |
| - * if there is a cycle in the list, then we can use two pointers travers the list. |
17 |
| - * |
18 |
| - * one pointer traverse one step each time, another one traverse two steps each time. |
19 |
| - * |
20 |
| - * so, those two pointers meet together, that means there must be a cycle inside the list. |
| 15 | + |
| 16 | +/** |
| 17 | + * Definition for singly-linked list. |
| 18 | + * struct ListNode { |
| 19 | + * int val; |
| 20 | + * ListNode *next; |
| 21 | + * ListNode(int x) : val(x), next(NULL) {} |
| 22 | + * }; |
21 | 23 | */
|
| 24 | +class Solution { |
| 25 | +public: |
| 26 | + bool hasCycle(ListNode *head) { |
| 27 | + return hasCycle04(head); |
| 28 | + return hasCycle03(head); |
| 29 | + return hasCycle02(head); |
| 30 | + return hasCycle01(head); |
| 31 | + } |
| 32 | + |
| 33 | + |
| 34 | + // using a map to store the nodes we walked |
| 35 | + bool hasCycle01(ListNode *head) { |
| 36 | + unordered_map<ListNode*,bool > m; |
| 37 | + ListNode* p = head; |
| 38 | + m[(ListNode*)NULL] = true; |
| 39 | + while( m.find(p) == m.end() ) { |
| 40 | + m[p] = true; |
| 41 | + p = p -> next; |
| 42 | + } |
| 43 | + return p != NULL; |
| 44 | + } |
| 45 | + |
| 46 | + // Change the node's of value, mark the footprint by a special value |
| 47 | + bool hasCycle02(ListNode *head) { |
| 48 | + ListNode* p = head; |
| 49 | + // using INT_MAX as the mark could be a bug! |
| 50 | + while( p && p->val != INT_MAX ) { |
| 51 | + p->val = INT_MAX; |
| 52 | + p = p -> next; |
| 53 | + } |
| 54 | + return p != NULL; |
| 55 | + } |
| 56 | + |
| 57 | + /* |
| 58 | + * if there is a cycle in the list, then we can use two pointers travers the list. |
| 59 | + * one pointer traverse one step each time, another one traverse two steps each time. |
| 60 | + * so, those two pointers meet together, that means there must be a cycle inside the list. |
| 61 | + */ |
| 62 | + bool hasCycle03(ListNode *head) { |
| 63 | + if (head==NULL || head->next==NULL) return false; |
| 64 | + ListNode* fast=head; |
| 65 | + ListNode* slow=head; |
| 66 | + do{ |
| 67 | + slow = slow->next; |
| 68 | + fast = fast->next->next; |
| 69 | + }while(fast != NULL && fast->next != NULL && fast != slow); |
| 70 | + return fast == slow? true : false; |
| 71 | + } |
| 72 | + |
| 73 | + // broken all of nodes |
| 74 | + bool hasCycle04(ListNode *head) { |
| 75 | + ListNode dummy (0); |
| 76 | + ListNode* p = NULL; |
| 77 | + |
| 78 | + while (head != NULL) { |
| 79 | + p = head; |
| 80 | + head = head->next; |
| 81 | + |
| 82 | + // Meet the old Node that next pointed to dummy |
| 83 | + //This is cycle of linked list |
| 84 | + if (p->next == &dummy) return true; |
| 85 | + |
| 86 | + p->next = &dummy; // next point to dummy |
| 87 | + } |
| 88 | + return false; |
| 89 | + } |
22 | 90 |
|
23 |
| -bool hasCycle(ListNode *head) { |
24 |
| - if (head==NULL || head->next==NULL) return false; |
25 |
| - ListNode* fast=head; |
26 |
| - ListNode* slow=head; |
27 |
| - do{ |
28 |
| - slow = slow->next; |
29 |
| - fast = fast->next->next; |
30 |
| - }while(fast != NULL && fast->next != NULL && fast != slow); |
31 |
| - return fast == slow? true : false; |
32 |
| -} |
| 91 | +}; |
0 commit comments