需要三个变量 res:上次的头结点,curr:当前的头节点,temp:原链表剩余节点的头节点。
while(原链表存在){
更新原链表值
当前头节点的下一个等于上次的头节点
更新上次的头节点为当前的头节点
当前头节点更新为原链表剩余节点的头结点
}
public class Solution {
public ListNode ReverseList(ListNode head) {
if(null==head || head.next==null)
return head;
ListNode temp = head.next;//保存原链条的节点
ListNode res = head;//保存分离出的头
res.next = null;
ListNode curr = temp;//保存已完成链条的头部
while(null != temp){
temp=temp.next;
curr.next=res;
res=curr;
curr=temp;
}
return res;
}
}