Confused on why my simple leetcode solution works - C#