代码实现:
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
//递归
public class Solution {
public ListNode ReverseList(ListNode head) {
if(head == null || head.next == null){
return head;
}
//递归,从最后开始反转链表
ListNode new_head = ReverseList(head.next);
//让下一个节点的next指向当前节点
head.next.next = head;
//让当前节点的next为空
head.next = null;
//只用返回头节点
return new_head;
}
}