Redian新闻
>
leetcode 一道简单题的疑问
avatar
leetcode 一道简单题的疑问# JobHunting - 待字闺中
v*p
1
Write a function to delete a node (except the tail) in a singly linked list
, given only access to that node.
Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third
node with value 3, the linked list should become 1 -> 2 -> 4 after calling
your function.
题目不难, 答案是:
public void deleteNode(ListNode node) {

if(node == null || node.next == null) return;

node.val = node.next.val;
node.next = node.next.next;
}
可是不明白为什么下面的不对:
public void deleteNode(ListNode node) {

if(node == null || node.next == null) return;

node = node.next;
}
avatar
h*d
2
Node.next 是object里的指针。
Node是外部指针,你不过是向后移了一位。

list

【在 v****p 的大作中提到】
: Write a function to delete a node (except the tail) in a singly linked list
: , given only access to that node.
: Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third
: node with value 3, the linked list should become 1 -> 2 -> 4 after calling
: your function.
: 题目不难, 答案是:
: public void deleteNode(ListNode node) {
:
: if(node == null || node.next == null) return;
:

avatar
r*r
3
第2种解法只改变了node的值(reference),使它由原来代表3变为4,但未能把3这个
object的内容(值)改变为4的内容(值)。

list

【在 v****p 的大作中提到】
: Write a function to delete a node (except the tail) in a singly linked list
: , given only access to that node.
: Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third
: node with value 3, the linked list should become 1 -> 2 -> 4 after calling
: your function.
: 题目不难, 答案是:
: public void deleteNode(ListNode node) {
:
: if(node == null || node.next == null) return;
:

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。