输入一个链表,反转链表后,输出新链表的表头。
public class Solution {
public ListNode ReverseList(ListNode head) {
if(head==null) return null;
ListNode pre = head;
ListNode next = head.next;
pre.next=null;// !!
while(next!=null){
ListNode temp = next.next;
next.next= pre;
pre= next;
next = temp;
}
return pre;
}
}
网友评论