/**
* Definition for singly-linked list. 链表反转-递归
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
//如果传的节点为空或者是最后一个节点,直接返回
if (head == null || head.next == null) {
return head;
}
ListNode new_head = reverseList(head.next);
head.next.next = head;
head.next = null;
return new_head;
}
}