Gluing pieces of scrambled text (challenge on CodeEval)
First of all I don't think the solution is correct. There could be multiple candidates to combine. Consider for example that the file contains, among other, the abc, bcd and bce strings, in this order. Your code unconditionally joins abc and bcd to produce abcd whereas the actual text has abce.

Also, it looks like you assume that at each iteration of the outer loop at least one string is removed. It is not necessarily so. If - due to a wrong candidate selection, see above - you happen to reach a dead end (unable to append or prepend any string), you are in the infinite loop. That explains TLE much better than the quadratic nature of the algorithm.

I am afraid you need some sort of backtracking. Compute the "successor" relation between fragments, and join them in a DFS with backtracking. Keep in mind that succession does not impose a strict order (e.g. aba and bab are each other successors).