Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LeetCode] 237. 删除链表中的节点 #95

Open
Animenzzzz opened this issue Sep 22, 2019 · 0 comments
Open

[LeetCode] 237. 删除链表中的节点 #95

Animenzzzz opened this issue Sep 22, 2019 · 0 comments

Comments

@Animenzzzz
Copy link
Owner

题目描述:

请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点。

示例 1:

输入: head = [4,5,1,9], node = 5
输出: [4,1,9]
解释: 给定你链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -> 9.

示例 2:

输入: head = [4,5,1,9], node = 1
输出: [4,5,9]
解释: 给定你链表中值为 1 的第三个节点,那么在调用了你的函数之后,该链表应变为 4 -> 5 -> 9.

说明:

链表至少包含两个节点。
链表中所有节点的值都是唯一的。
给定的节点为非末尾节点并且一定是链表中的一个有效节点。
不要从你的函数中返回任何结果。

解题思路:刚开始看到参数没有头结点,一下没反应过来。只要讲后一个节点的值覆盖,然后删除后一个节点就行

C++解题:

class Solution {
public:
    void deleteNode(ListNode* node) {
        int next_val = node->next->val;
        node->val = next_val;
        node->next = node->next->next;
    }
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant