Your solution is same to mine. Because after one loop,the next two node(after the prev) will have already been swaped. So u need to make the prev move to its next and next node! But the first(vari name) presents the next node of prev, if u make prev= first , the swap will happen again.For example,
-1->1->2->3->4; After the first loop, the list will be -1->2->1->3->4; so the prev must direct to 3 not 2. Am i clear?
Hi shenhualonga, I think memorization is theoretically helpful. For example, given a string "bba", there are two ways of reaching a, either by "b|b|a" or by "bb|a". If we use memorization, the palindrome checking for "a" can be shared.
But on the other hand, the complexity is O(n * 2 ^ (n-1)), since there are 2 ^ (n-1) ways of dividing up the string and the palindrome checking for each string is O(n) time altogether. Therefore palindrome checking is not critical, since the problem is exponential anyway. You need to generate that many strings anyway.