アルゴリズム:24。リンクリスト内のノードをペアで交換します

:LeetCodeの完全なコレクションについては、を参照してくださいLeetCode Githubのコレクション

トピック

24.リンクリスト内のノードを交換し
ますhttps://leetcode-cn.com/problems/swap-nodes-in-pairs/

/*
 * @lc app=leetcode.cn id=24 lang=java
 *
 * [24] 两两交换链表中的节点
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    
    
    public ListNode swapPairs(ListNode head) {
    
    
        // check edge
        if (head == null || head.next == null) {
    
    
            return head;
        }

        ListNode n = head.next;
        head.next = swapPairs(head.next.next);
        n.next = head;
        
        return n;
    }
}
// @lc code=end

おすすめ

転載: blog.csdn.net/zgpeace/article/details/114776761