static class Node { private Node next; private int val; public Node(Node next, int val) { this.next = next; this.val = val; } } public static void main(String[] args) { Node nodeA = new Node(null, 1); Node nodeB = new Node(nodeA, 2); Node nodeC = new Node(nodeB, 3); Node nodeD = new Node(nodeC, 4); Node header = new Node(nodeD, 5); Node pre = null; Node cur = header; Node next = cur.next; while (cur.next != null) { cur.next = pre; pre = cur; cur = next; next = cur.next; } cur.next = pre; while (cur != null) { System.out.println(cur.val); cur = cur.next; } }