static class Node { private Node next; private int val; public Node(Node next, int val) { this.next = next; this.val = val; } public Node getNext() { return next; } public void setNext(Node next) { this.next = next; } public int getVal() { return val; } public void setVal(int val) { this.val = val; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Node node = (Node) o; return val == node.val && Objects.equals(next, node.next); } } public static void main(String[] args) { Node node1 = new Node(null, 1); Node node2 = new Node(node1, 2); Node node3 = new Node(node2, 3); Node node4 = new Node(node3, 4); Node node5 = new Node(node4, 5); Node node6 = new Node(node5, 6); Node node7 = new Node(node6, 7); Node node8 = new Node(node7, 8); Node tail = new Node(node8, 9); int n = 2; Node slow = tail; //fast先走N步,然后slow与fast同时走 Node fast = tail; for (int i = 0; i < n; i++) { fast = fast.next; } while (fast.next != null) { fast = fast.next; slow = slow.next; } System.out.println(slow.val); }