输入一个链表,反转链表后,输出新链表的表头。
方法:头插入
public class Solution {
public ListNode ReverseList(ListNode head) {
if(head==null)
return null;
ListNode res=new ListNode(-1);
res.next=null;
while(head!=null){
ListNode temp=head;
head=head.next;
temp.next=res.next;
res.next=temp;
}
return res.next;
}
}