public static void main(String[] args) {
Node head = new Node(1);
Node node1 = new Node(2);
Node node2 = new Node(3);
Node node3 = new Node(4);
Node node4 = new Node(5);
head.next = node1;
node1.next = node2;
node2.next = node3;
node3.next = node4;
System.out.println("head = " + head);
Node node = reverseLinkedList(head);
System.out.println("node.toString() = " + node.toString());
}
public static Node reverseLinkedList(Node head){
if(head.next == null){
return head;
}
Node newHead = reverseLinkedList(head.next);
head.next.next = head;
head.next = null;
return newHead;
}
static class Node {
int value;
Node next;
public Node(int value, Node next) {
this.value = value;
使用递归的方式实现单链表反转
于 2021-11-17 22:29:10 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)