LeetCode-Problem 24:交换链表中相邻结点

算法

给定一个链表,对每两个相邻的结点作交换并返回头节点。

例如:

给定 1->2->3->4,你应该返回 2->1->4->3。

算法实现

    public ListNode swapPairs(ListNode head) {
        if ((head == null) || (head.next == null)) {
            return head;
        }
        ListNode header = head.next;
        head.next = swapPairs(head.next.next);
        header.next = head;
        return header;
    }

猜你喜欢

转载自blog.csdn.net/kangkanglou/article/details/79867325