Simple Swift Solution
CodeCode
![Screenshot 2025-04-02 at 1.41.48 PM.png](https://assets.leetcode.com/users/images/a10ebd94-6fec-4d45-8a81-d29c1eed81df_1743590539.1283495.png)


# Code
```swift []
class Solution {
    func numberOfChild(_ n: Int, _ k: Int) -> Int {
        var pointer = 0, reverse = true
        for i in 1...k {
            if pointer == 0 || pointer == n - 1 {
                reverse = !reverse
            }
            if reverse {
                pointer -= 1
            } else {
                pointer += 1
            }
        }
        return pointer
    }
}
```

![Screenshot 2025-04-02 at 2.46.46 PM.png](https://assets.leetcode.com/users/images/a8bbc5b5-d6bd-4faf-8fea-3c24c2e76248_1743594451.302718.png)

# Code
```swift []
class Solution {
    func numberOfChild(_ n: Int, _ k: Int) -> Int {
        let period = 2 * (n - 1)
        let positionInPeriod = k % period
        return positionInPeriod < n ? positionInPeriod : period - positionInPeriod
    }
}
```
['Math', 'Swift', 'Simulation']