Most elegant solution


  • -1
    C

    class Solution {
    public:
    ListNode *detectCycle(ListNode head) {
    ListNode
    slower = head, *faster = head;
    while (faster && faster->next) {
    slower = slower->next;
    faster = faster->next->next;
    if (faster == slower)
    break;
    }
    if (!faster || !faster->next) return NULL;
    while (head != slower) {
    head = head->next;
    slower = slower->next;
    }
    return head;
    }
    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.