迭代写法
public ListNode reverseList(ListNode head) {
if(head==null||head.next==null){
return head;
}
ListNode a=head,b=a.next;
while(b!=null){
ListNode c=b.next;
b.next=a;
a=b;
b=c;
}
head.next=null;
return a;
}
递归写法
public ListNode reverseList(ListNode head) {
if(head==null||head.next==null){
return head;
}
ListNode tail=reverseList(head.next);
head.next.next=head;
head.next=null;
return tail;
}