迭代方式:
public ListNode reverseList(ListNode head) {
ListNode newHead=null;
while(head!=null)
{
ListNode next=head.next;
head.next=newHead;
newHead=head;
head=next;
}
return newHead;
}
}
递归方式:
public ListNode reverseList(ListNode head) {
return reverselist(head,null);
}
public ListNode reverselist(ListNode head,ListNode newhead)
{
if(head==null)
return newhead;
ListNode next=head.next;
head.next=newhead;
return reverselist(next,head);
}