diff options
add Seminar06
1 files changed, 33 insertions, 0 deletions
| -rw-r--r-- | Seminar06/linkedListCycle.cpp | 33 |
diff --git a/Seminar06/linkedListCycle.cpp b/Seminar06/linkedListCycle.cpp new file mode 100644 index 0000000..4aa33c6 --- /dev/null +++ b/Seminar06/linkedListCycle.cpp @@ -0,0 +1,33 @@ +#include"../Seminar04/linked-list-utils.h" + +bool hasCycle(LinkedListNode<int>* head) { + LinkedListNode<int> *fast = head; + LinkedListNode<int> *slow = head; + + while (fast && fast->next && slow) { + fast = (fast->next)->next; + slow = slow->next; + + if (fast == slow) + return true; + } + + return false; +} + +int main() { + using Node = LinkedListNode<int>; + + Node* last = new Node(6); + + Node* list = new Node(1, + new Node(2, + new Node(3, + new Node(4, + new Node(5, + last))))); + + last->next = list->next->next; + + hasCycle(list); +}
\ No newline at end of file |
