leetcode 206. 反转链表
https://leetcode-cn.com/problems/reverse-linked-list/
递归解法如下:
1 2 3 4 5 6 7 | public ListNode reverseList(ListNode head) { if (head == null || head.next == null ) return head; ListNode p = reverseList(head.next); head.next.next = head; head.next = null ; return p; } |
问题1:在调试过程中,为什么当执行到
1 | head.next.next = head; |
时会报内存溢出错误?如图所示
问题2:这整个执行方法中,都是对head进行操作:
head.next.next = head;
head.next = null;
并没有对p进行赋值操作,为什么return p 就能获得反转链表的值?
麻烦老师解答一下,非常感谢!