单链表翻转是一道很基本的算法题
一般的处理办法是设定三个指针,将第二个指针的next指向第一个指针,然后逐渐后移
class Node{
int value;
Node next;
}
Node resetList(Node head){
Node p1, p2, p3;
if(head == null || head.next == null){
return head;
}
p1 = head;
p2 = head.next;
while(p2 != null){
p3 = p2.next; //保存记录下一个值
p2.next = p1; //指向下一个点
p1 = p2;
p2 = p3;
}
head.next = null;
head = p1;
return head;
}