我试图了解反向链接列表的递归方式。
public ListNode reverseList2(ListNode head) {
if(head == null || head.next == null) {
return head;
}
ListNode newHead = reverseList2(head.next);
head.next.next = head;
head.next = null;
return newHead;
}
反向链表
1->2->3->null
答案是
3->2->1->null
据我了解,最后一个节点应该指向空。但是在这个递归函数中,当它反转最后一个节点时,它并没有将它指向空。可以吗,最后一个节点不指向空?还是我错过了什么?