声明一个节点pre初始化为null,声明next节点保存当前结点的next节点,然后令当前结点的next节点指向pre,再将当前结点赋给pre,最后将next节点赋给当前节点,直到当前结点为空遍历结束,返回pre节点。
public ListNode reverseList(ListNode head) {
if(head==null||head.next==null)
return head;
ListNode pre=null;
while(head!=null) {
ListNode next=head.next;
head.next=pre;
pre=head;
head=next;
}
return pre;
}