package com.zjl.dataStract;
public class InversionLinkList {
public static void main(String[] args) {
Node node1 = new Node(null, 44);
Node node2 = new Node(node1, 55);
Node node3 = new Node(node2, 66);
Node node4 = new Node(node3, 77);
Node node5 = new Node(node4, 88);
Node node0 = new Node(node5, 99);
Node p = node0;
while (p != null) {
System.out.print(p.data + " --> ");
p = p.next;
}
p = reInversion(node0);
System.out.println();
while (p != null) {
System.out.print(p.data + " --> ");
p = p.next;
}
}
public static class Node {
public Node(Node next, Integer data) {
this.next = next;
this.data = data;
}
public Node next;
public Integer data;
}
public static Node inversion(Node node) {
Node p = node;
Node q = p.next;
p.next = null;
Node t;
while (q != null) {
t = q.next;
q.next = p;
p = q;
q = t;
}
return p;
}
public static Node reInversion(Node node) {
if (node == null || node.next == null) {
return node;
}
Node inversionNode = reInversion(node.next);
node.next.next = node;
node.next = null;
return inversionNode;
}
}
|