结点定义
class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
非递归方法
public ListNode reverse(ListNode head){
ListNode pre = null;
ListNode next = null;
while(head !=null){
next = head.next;
head.next = pre;
pre = head;
head = next;
}
return pre;
}
递归方法
public ListNode reverse(ListNode head){
if(head==null || head.next==null)
return head;
ListNode tmp = head.next;
ListNode newHead = reverse(head.next);
tmp.next = head;
head.next = null;
return newHead;
}