原文地址:https://blog.csdn.net/zdj666666/article/details/79898159?utm_source=blogxgwz1
单链表反转
class Solution{
//在这里仿佛是一根虎节鞭,从头到尾一节节进入这个函数中来!!!
ListNode ReverseList(ListNode head){
ListNode pre=null;
ListNode next=null;
if(head == null){
return null;
}
//我就代表一直让鞭子传入进来,直到最后。
while(head!=null){
next=head.next; //把下一个值保存下来。
head.next=pre; // 将head的下一个值指向head的前一个值,实现反转
pre=head; // 指向反转后,将pre移动指向head
head = next;
}
return pre;
}