迭代法
将每个结点的next指针改为指向前一个元素。
public ListNode reverseList(ListNode head){
ListNode prev = null;
ListNode curr = head;
while(curr != null){
ListNode nextTemp = curr.next;
curr.next = prev;
prev = curr;
curr = nextTemp;
}
return prev;
}
递归法
public ListNode reverseList(ListNode head){
if(head==null || head.next==null)
return head;
ListNode p = reverseList(head.next);
head.next.next = head;
return p;
}