剑指Offer 24、反转链表
class Solution {
public ListNode reverseList(ListNode head) {
if(head == null) return head;
ListNode pre = null;
/**
使用while循环进行指针的修改,直到head.next == null,说明当前head是原
链表的最后一个节点,那么直接head.next = pre; return head;就是答案。
**/
while(head.next != null) {
ListNode next = head.next;
head.next = pre;
pre = head;
head = next;
}
head.next = pre;
return head;
}
}