时间限制:1秒 空间限制:32768K

题目描述
输入一个链表,反转链表后,输出新链表的表头。

思路:
利用递归解决即可,首先考虑两个节点的情况,将头结点的next指针指向的节点进行反转,然后,将头结点的next指针置空,放到反转后的next节点尾部即可

/* 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 next = head.next;
        ListNode res = ReverseList(next);
        head.next = null;
        next.next = head;
        return res;
    }
}