package me.wayne.ds;

public class Node {

    public Node next;
    public int value;

    public Node(int value) {
        this.value = value;
    }

    public static void testReverse() {
        Node head = new Node(1);
        head.next = new Node(2);
        head.next.next = new Node(3);
        head.next.next.next = new Node(4);
        Node reverse = Node.reverse(head);
        while (reverse != null) {
            System.out.println(reverse.value);
            reverse = reverse.next;
        }
    }

    public static Node reverse(Node node) {
        if (node == null || node.next == null) {
            return node;
        }
        return reverseInner(null, node);
    }

    private static Node reverseInner(Node p, Node q) {
        if (q == null) {
            return p;
        }

        Node temp = q.next;
        q.next = p;

        return reverseInner(q, temp);
    }
}

@@ -342,23 +342,8 @@
     
- Node reverse =
  Nod
@@ -351,16 +351,20 @@
 .reverse
+Half
 (head);%0A
@@ -375,126 +375,24 @@
     
-while (reverse != null) %7B%0A            System.out.println(reverse.value);%0A            reverse = reverse.next;%0A        %7D
+printNode(head);
 %0A   
@@ -425,16 +425,25 @@
  reverse
+Recursive
 (Node no
@@ -785,11 +785,1154 @@
 );%0A    %7D
+%0A%0A    private static Node reverseNonRecursive(Node head) %7B%0A        if (head == null %7C%7C head.next == null) %7B%0A            return head;%0A        %7D%0A        Node pre = null;%0A        Node next = null;%0A        while (head != null) %7B%0A            next = head.next;%0A%0A            head.next = pre;%0A            pre = head;%0A            head = next;%0A        %7D%0A        return pre;%0A    %7D%0A%0A    private static void reverseHalf(Node head) %7B%0A        if (head == null %7C%7C head.next == null) %7B%0A            return;%0A        %7D%0A        //%E6%9E%84%E9%80%A0%E4%B8%80%E4%B8%AA%E4%B8%B4%E6%97%B6%E7%BB%93%E7%82%B9%0A        Node pre = new Node(0);%0A        pre.next = head;%0A%0A        Node half = head;%0A        Node tail = head;%0A        while (tail.next != null) %7B%0A            //%E5%BF%AB%E6%85%A2%E6%8C%87%E9%92%88%EF%BC%8Ctail%E8%B5%B0%E4%B8%A4%E6%AD%A5%EF%BC%8Chalf%E8%B5%B0%E4%B8%80%E6%AD%A5%0A            tail = tail.next;%0A            half = half.next;%0A            pre = pre.next;%0A%0A            if (tail.next != null) %7B%0A                tail = tail.next;%0A            %7D%0A        %7D%0A%0A        pre.next = null;%0A        reverseNonRecursive(half);%0A        pre.next = tail;%0A    %7D%0A%0A    private static void printNode(Node node) %7B%0A        while (node != null) %7B%0A            System.out.println(node.value);%0A            node = node.next;%0A        %7D%0A    %7D
 %0A%7D%0A

完成reverseHalf.