题目描述
输入一个链表,反转链表后,输出新链表的表头
非递归版
public ListNode ReverseList(ListNode head) {
if(head==null)
return null;
ListNode temp=null;
ListNode pre=null;
while(head!=null){
temp=head.next;
head.next=pre;
pre=head;
head=temp;
}
return pre;
}
递归版
public ListNode ReverseList(ListNode head) {
if(head==null || head.next==null) return head;
ListNode pre = ReverseList(head.next);
head.next.next=head;
head.next=null;
return pre;
}