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;
}