Case 1: Waiting for HH
$A$ = average nb. of flips to get HH after T or nothing.
$B$ = average nb. of flips to get HH after H.
A is 1 flip, plus B if you get H and continue, or plus A if you get T and restart:
$A = 1 + {1\over2}B + {1\over2}A$
B is 1 flip, and you are done if you get H, or plus A if you get T and restart:
$B = 1 + {1\over2}0 + {1\over2}A$
This resolves to
$B = 1 + {1\over 2}A$
$A = 1 + {1\over 2} (1 + {1\over 2} A) + {1\over2} A$
$A = 1 + {1\over 2} + {1\over 4} A + {1\over 2} A$
$A = 6$
The expected number of flips to get HH is 6.
Case 2: Waiting for HT
$A$ = average nb. of flips to get HT after T or nothing.
$B$ = average nb. of flips to get HT after H.
A is 1 flip, plus B if you get H and continue, or plus A if you get T and restart:
$A = 1 + {1\over2}B + {1\over2}A$
B is 1 flip, and you are done if you get T, or plus B if you get H and wait for T again:
$B = 1 + {1\over 2}0 + {1\over 2}B$
This resolves to:
$B = 1 + {1\over 2}B$
$B = 2$
$A = 1 + {1\over 2}2 + {1\over 2}A = 2 + {1\over 2}A$
$A = 4$
The expected number of flips to get HT is 4.
Comparison
The difference comes from the fact that in the first case, if you get the wrong 2nd result, you start all over, while in the second case you continue with one good result.