3. Given that $n$ is a positive integer, and the real number $x$ satisfies
$$
|1-| 2-|3-\cdots|(n-1)-|n-x||\cdots|||=x \text {. }
$$

Find the value of $x$.
Hint: By giving $n$ special values, we can find the pattern: when $n=4 k+1$ or $n=4 k+2(k \in \mathbf{N})$, the solution set of the equation is $\left\{\frac{1}{2}\right\}$; when $n=4 k$ or $n=4 k+3(k \in \mathbf{N})$, the solution set of the equation is $\{x \in \mathbf{R} \mid 0 \leqslant x \leqslant 1\}$.