Reverse a singly linked list.
題意:反轉單鏈表
解題思路:一直取下一個節點作爲頭部
代碼:
public class Solution {
public ListNode reverseList(ListNode head) {
return reverseListInt(head,null);
}
public ListNode reverseListInt(ListNode head,ListNode newHead){
if(head == null){
return newHead;
}
ListNode temp = head.next;
head.next = newHead;
return reverseListInt(temp,head);
}
}