先写的单的,哪天把双链表逆置写一下。
![](/images/smiles/icon_evil.gif)
public class LinkedListReverse { /** * 单链表逆置 * * @author aaron-han * */ public static void main(String[] args) { Node a = new Node("NodeA"); Node b = new Node("NodeB"); Node c = new Node("NodeC"); Node d = new Node("NodeD"); Node e = new Node("NodeE"); a.next = b; b.next = c; c.next = d; d.next = e; System.out.println("original:"); printNodes(a); Node head = reverse(a); System.out.println("after reverse:"); printNodes(head); } public static Node reverse(Node node) { if (node == null) { return node; } Node head = node; Node pre = null; Node next = head.next; while (next != null) { head.next = pre; pre = head; head = next; next = next.next; } head.next = pre; return head; } public static void printNodes(Node node) { for (; node != null; node = node.next) { System.out.println(node.name); } } static class Node { String name; Node next; Node(String name) { this.name = name; } } }