/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode ReverseList(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode pre = null;
ListNode cur = head;
ListNode tmp = null;
while (cur != null){
//先保存tmp
tmp = cur.next;
//反转链表
cur.next = pre;
//整体后移
pre = cur;
cur = tmp;
}
//cur 此时为null,输出pre
return pre;
}
}