【Leetcode237】关关的刷题日记 71–Leetcode237.Delete Node in a Linked List

时间:2022-05-10
本文章向大家介绍【Leetcode237】关关的刷题日记 71–Leetcode237.Delete Node in a Linked List,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

关关的刷题日记71 – 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.

思路

题目的意思是让我们删除指定节点(该节点不是尾节点),一般情况下我们都是通过前一个节点来操作,但是这个题目只给出了当前节点的指针。所以我们把下一个节点的值赋给当前节点,然后把下一个节点删除了,就相当于删掉了“当前节点”了。题目也特别强调了被删除的节点不是尾节点,如果要有可能是尾节点,这题目就没法做了。

class Solution {
public:
    void deleteNode(ListNode* node) {
        node->val=node->next->val;
        node->next=node->next->next;
    }
};

人生易老,唯有陪伴最长情,加油!

以上就是关关关于这道题的总结经验,希望大家能够理解,有什么问题可以在我们的专知公众号平台上交流或者加我们的QQ专知-人工智能交流群 426491390,也可以加入专知——Leetcode刷题交流群(请先加微信小助手weixinhao: Rancho_Fang)。