【发布时间】:2020-08-31 07:11:08
【问题描述】:
我知道我的代码完全错误,但我不知道我哪里做错了,
谁能指出并解释我做错了什么?
public ListNode reverseList(ListNode head) {
if (head == null) {
return head;
}
ListNode prev = null;
ListNode current = head;
ListNode nextNode = head.next;
while (nextNode != null) {
prev = current;
current = nextNode;
current.next = prev;
nextNode = nextNode.next;
System.out.println(nextNode.val);
}
return current;
}
【问题讨论】:
-
看你执行的顺序
current.next = prev;和nextNode = nextNode.next;-current和nextNode在这两行之前指的是同一个节点,那么你认为执行时会发生什么他们 -
这是 Java 吗?如果是这样,您可能希望将标签添加到问题中。
标签: algorithm data-structures linked-list