Analyzing Will Ferrell's "I Thought" joke

Solution 1:

The best way to understand something like this is to replace each occurrence of the repeated word with either a synonym, or something that matches its part of speech:

Just came up with an idea, but the idea I came up with wasn't the idea I believed I came up with.

As you can see, removing the last "I thought" either changes the meaning, or results in a contradiction:

  • Just came up with an idea, but the idea I came up with wasn't the idea I believed – inventing an idea you don't believe is quite different from misremembering what idea you invented.

  • Just came up with an idea, but the idea I came up with wasn't the idea I came up with – this is a contradiction, and doesn't make a lot of sense. (Well, not that the original makes a whole lot of sense, either.)

I don't know if there's an official term for this sort of phrase. Depending on the repeated word, it could be considered a tongue-twister. For the famous buffalo example mentioned by MrHen in the comments, Wikipedia just says it's "an example of how homonyms and homophones can be used to create complicated linguistic constructs."

Solution 2:

If you remove the last "I thought", you get a contradiction in

the thought I thought wasn't the thought I thought

R D Laing called this sort of thing Knots. One example is

He is devoured by his devouring fear of being devoured by her devouring desire for him to devour her.