gpt4 book ai didi

java - 反向链表Java内存

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:13:55 25 4
gpt4 key购买 nike

<分区>

我正在做这个 leetcode 问题,但我不明白为什么这个解决方案不起作用。它似乎只返回 head 元素。谢谢

 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode reverseList(ListNode head) {
ListNode curr = null;
ListNode lst = null;
while (head != null)
{
curr = head;
curr.next = lst;
lst = curr;
head = head.next;
}

return curr;

}
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com