leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0142.cpp (565B)


0 class Solution { 1 public: 2 ListNode *detectCycle(ListNode *head) { 3 if (!head) return nullptr; 4 5 ListNode *slow, *fast; 6 fast = slow = head; 7 while (fast->next && fast->next->next) { 8 fast = fast->next->next; 9 slow = slow->next; 10 if (fast == slow) { 11 fast = head; 12 while (fast != slow) { 13 fast = fast->next; 14 slow = slow->next; 15 } 16 return slow; 17 } 18 } 19 return nullptr; 20 } 21 };