递归方法如下:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
if (head == null || head.next == null) return head;
ListNode H = reverseList(head.next);
head.next.next = head;
head.next = null;
return H;
}
}
其中H其实是一个新的指针,而不是新的链表,始终只有一个链表
理解图示