LeetCode [206] reverse list

To the list is upside down, you can define two pointers, constantly running back, p1 = head, p2 = head.next, so you can p2.next = p1, p2 will receive this in front of the p1.

The problem is that the back of the chain is disconnected, a pointer can be redefined temp, temp = p2.next, the sentence to the front p2.next = p1, and then, can be p1 = p2, p1 so on

Still head position, p2 = temp, as well as key is that there must be p1.next = null in front, or will fall into error.

Last is the return p1.

Guess you like

Origin www.cnblogs.com/wzwi/p/10942974.html