v****p 发帖数: 53 | 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;
} | h********d 发帖数: 109 | 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; :
| r********r 发帖数: 208 | 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; :
|
|