LeetCode題解:Reverse Linked List

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);
    }
}
發佈了235 篇原創文章 · 獲贊 8 · 訪問量 24萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章