As suggested at the time in a now deleted answer, this can be solved using inclusion–exclusion.
There are $n-1$ conditions, one for each pair of neighbours.
We need to count the ways of violating any subset of these conditions. Say we want to violate $k$ of them. We can tell the $k$ pairs of neighbours who stay together to hold hands, and then we can still permute the line in $(n-k)!$ ways. But any consecutive run of neighbours holding hands can also reverse their order; so we also get a factor $2^r$, where $r$ is the number of such runs. There are $\binom{k-1}{r-1}$ ways to distribute $k$ pairs over $r$ runs and $\binom{n-k}r$ ways to position those runs in the line. Thus, by inclusion–exclusion, there are
$$
n!+\sum_{k=1}^{n-1}(-1)^k\sum_{r=1}^k\binom{n-k}r\binom{k-1}{r-1}(n-k)!2^r
$$
permutations that fulfil all the conditions.
Here’s Java code that performs this computation. The result is OEIS sequence A002464, for which a number of references and expressions are given, including the one above, contributed by Max Alekseyev.