From e2d1999caeab97138791842ed3d2e7a561533ea1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=96=87=E8=B5=B5?= <1392936858@qq.com> Date: Wed, 3 Jul 2019 01:50:01 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=92=E5=BD=92=E5=AD=A6=E4=B9=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../leetcode/recursive/SwapNode.java | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/main/java/com/ximo/datastructuresinaction/leetcode/recursive/SwapNode.java diff --git a/src/main/java/com/ximo/datastructuresinaction/leetcode/recursive/SwapNode.java b/src/main/java/com/ximo/datastructuresinaction/leetcode/recursive/SwapNode.java new file mode 100644 index 0000000..5648c2c --- /dev/null +++ b/src/main/java/com/ximo/datastructuresinaction/leetcode/recursive/SwapNode.java @@ -0,0 +1,68 @@ +package com.ximo.datastructuresinaction.leetcode.recursive; + +import java.util.StringJoiner; + +/** + * 两两交换链表中的节点 + * + * 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。 + * + * 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。 + * + * 示例: + * + * 给定 1->2->3->4, 你应该返回 2->1->4->3. + * + * @author xikl + * @date 2019/7/3 + */ +public class SwapNode { + + private static class ListNode { + int value; + private ListNode tail; + + public ListNode(int x) { + this.value = x; + } + + } + + public static ListNode swapPairs(ListNode head) { + if (head == null || head.tail == null) { + return head; + } + + ListNode tail = head.tail; + head.tail = swapPairs(tail.tail); + + tail.tail = head; + + // 此时他就是头结点 + return tail; + } + + public static void main(String[] args) { + ListNode head = new ListNode(1); + head.tail = new ListNode(2); + head.tail.tail = new ListNode(3); + head.tail.tail.tail = new ListNode(4); + print(head); + + ListNode result = swapPairs(head); + print(result); + + } + + private static void print(ListNode head) { + ListNode cur = head; + StringJoiner joiner = new StringJoiner(" -> ", "{", "}"); + while (cur != null) { + joiner.add(String.valueOf(cur.value)); + cur = cur.tail; + } + System.out.println(joiner.toString()); + } + + +}