代码说明
class Solution {
public ListNode reverseList(ListNode head) {
ListNode pre = null; //保存前一个节点
ListNode next = null; // 临时保存下一个节点
while(head != null) {
next = head.next; // 保存下一个节点,用来更新head
head.next = pre; // 当前节点指向前一个节点
pre = head; // pre变为当前节点
head = next; // 更新head,赋值为之前保存的next
// 此时链表断开,前一段是已经翻转好的链表,pre是前一段的头结点
// head 是第二段待翻转的链表的头结点,循环直到结束
}
return pre;
}
}