C# Different solution (No linkedlist loop needed) O(n) time complexity, o(1) space.


  • 1
    public class Solution 
    {
        public int FindDuplicate(int[] nums) 
        {
            int index1 = 0;
            int index2 = 0;
            while(true)
            {
                index2 = nums[index1];
                if (index2 == -1)
                    return index1;
                nums[index1] = -1;
                index1 = index2;
            }
            return -1;
        }
    }```

Log in to reply
 

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