We give two fairly full solutions. The first involves finding the probability distribution of $X$. The second bypasses that, and works directly with expectations. With either approach, it is a good idea to separate out the trivial cases $p=0$ and $p=1$.
Analysis via the distribution function: If you really want the distribution function of $X$, break things up into cases. It is clear that if $n<2$, then $P(X=n)=0$.
Given that at first you get a head (probability $p$) then the probability that $X=n$ is computed as follows. You need your initial head to be followed by $n-2$ tails, and then a head. This has probability $(1-p)^{n-2}p$.
Given that at first you get a tail (probability $1-p$) then the probability that $X=n$ is computed as follows. You need your initial tail to be followed by $n-2$ heads, and then a tail. This has probability $p^{n-2}(1-p)$.
Put things together. We get $P(X=n)=p(1-p)^{n-2}p +(1-p)p^{n-2}(1-p).$ This can be simplified a bit. Not much.
To compute $E(X)$ from the probability distribution of $X$, note that $E(X)=\sum_{n=2}^\infty n\left[p(1-p)^{n-2}p +(1-p)p^{n-2}(1-p)\right].$ You can now find simplify by handling separately $\sum_{n=2}^\infty n p(1-p)^{n-2}p$ and the other half. These two sums are closely related to the sums you need to evaluate when you find the mean of a random variable that has geometric distribution. It is likely that the mean of the geometric distribution has already been done in your course.
Direct calculation of expectation: Either you first get a head (probability $p$) or a tail (probability $1-p$). Either way, it has cost you $1$ toss.
Given that you got a head, we now are waiting for another head. The mean waiting time for a success where the probability of success is $p$ is $\dfrac{1}{p}$. (This comes from information about the geometric distribution, which likely has been dealt with in your course.)
Given that you got a tail, we now are waiting for another tail. The mean waiting time for a success where the probability of success is $1-p$ is $\dfrac{1}{1-p}$. It follows that $E(X)=1+p\frac{1}{p}+(1-p)\frac{1}{1-p}=3.$ Please note that the above analysis does not work if $p=0$ or $p=1$. In those two cases (with probability $1$), the game last for $2$ tosses only, and therefore $E(X)=2$.