LeetCode 206反転チェーンジャバ再帰

1811 ワード

テーマの説明
シングルチェーン表を反転します。
例:
  : 1->2->3->4->5->NULL
  : 5->4->3->2->1->NULL
問題の分析
  • 例えば、1->>2->>3->4->5->NULLは、NULLに変更される。
  • コード
    class Solution {
        public ListNode reverseList(ListNode head) {
            if (head == null || head.next == null) return head;
            ListNode node = reverseList(head.next);
            head.next.next = head;
            head.next = null;
            return node;
        }
    }
    
    参照
    LeetCodeユーザー陳楽楽楽楽C++解法