4. Given $a_{1}=1, a_{n+1}=2 a_{n}+\frac{n^{3}-2 n-2}{n^{2}+n}$. Then the general term formula of $\left\{a_{n}\right\}$ is $\qquad$ .
4. $a_{n}=2^{n-1}+\frac{1-n^{2}}{n}$.

From the given condition,
$$
a_{n+1}+\frac{n(n+2)}{n+1}=2\left[a_{n}+\frac{(n+1)(n-1)}{n}\right] \text {. }
$$

Let $b_{n}=a_{n}+\frac{(n+1)(n-1)}{n}$. Then $b_{1}=1$.
Therefore, $\left\{b_{n}\right\}$ is a geometric sequence with the first term 1 and common ratio 2.
Thus, $b_{n}=2^{n-1}$.
Hence, $a_{n}=2^{n-1}-\frac{n^{2}-1}{n}=2^{n-1}+\frac{1-n^{2}}{n}$.