Reverse a singly linked list.
这道题有Recursive和Iterative两种解法,这里先写Recursive的
需要好好思考一下。
public ListNode reverse(ListNode head) {
// write your code here
ListNode prev = null;
ListNode curr = head;
while (curr != null) {
ListNode currnext = curr.next;
curr.next = prev;
prev = curr;
curr = currnext;
}
return prev;
}