LeetCode笔记:237. Delete Node in a Linked List

题目:

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.

大意:

写一个函数来删除一个简单链表中的一个节点(除了尾节点),只给出这个节点。

假设有一个链表 1->2->3->4,并给你第三个值为3的节点,在调用你的函数之后这个链表应该变成1->2->4。

思路:

一般我们删除一个链表节点,直接将其上一个节点的next指向其下一个节点就可以了,但是这里只给出了该节点本身,也就是说你只能获取到该节点本身以及其下一个节点。那么就只能将该节点直接变成下一个节点了,将其值设为下一个节点的值,将其next指向下一个节点的next,就可以了。

代码(Java):

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public void deleteNode(ListNode node) {
        node.val = node.next.val;
        node.next = node.next.next;
    }
}

代码(C++):

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public void deleteNode(ListNode node) {
        *node = *node->next;
    }
}

疑问:

用Java像C++那样,直接将 node = node.next 将不能ac,必须对其val和next分别设置,这是为什么呢?希望高手帮忙解答一下~

版权所有:http://blog.csdn.net/cloudox_

时间: 2024-10-22 11:30:12

LeetCode笔记:237. Delete Node in a Linked List的相关文章

【一天一道LeetCode】#237. Delete Node in a Linked List

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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

[LeetCode] 237. Delete Node in a Linked Lis解题小结

题目: 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 -

【LeetCode】237 - Delete Node in a Linked List

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 ->

leetCode 237. Delete Node in a Linked List 链表

237. Delete Node in a Linked List 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

237. Delete Node in a Linked List(C++)

237. Delete Node in a Linked Lis t 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

leetcode:237 Delete Node in a Linked List-每日编程第四题

Delete Node in a Linked List Total Accepted: 47385 Total Submissions: 107608 Difficulty: Easy 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 ->

LeetCode (237):Delete Node in a Linked List

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 ->

[leetcode] 237. Delete Node in a Linked List 解题报告

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 ->

[LeetCode] 237. Delete Node in a Linked List 解题思路

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 ->