zoukankan      html  css  js  c++  java
  • 【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;
    2. 首先创建一个指向节点3的指针,然后将节点2赋值为节点3,此时的链表状态如第二行所示;
    3. 最后别忘记将节点3删除(通过删除之前创建的指针)。

    代码:

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        void deleteNode(ListNode* node) {
            ListNode *next = node->next;
            *node = *next;
            delete next;
        }
    };
  • 相关阅读:
    入门指引之永久素材
    入门指引之上传临时素材
    入门指引之查看accesstoken
    java中的左移 右移
    病狗问题
    leetcode 几何题 位运算 面试编程
    CNN网络参数
    python学习整理
    JAVA问题整理
    计算机网络整理
  • 原文地址:https://www.cnblogs.com/jdneo/p/4737112.html
Copyright © 2011-2022 走看看