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.

分析

题目要求删除指定结点,但是给定参数只有目标结点。

转移思维,改成拷贝目标结点后结点的值,然后删除目标结点的后一结点。

AC代码

/**
 * 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) {
        if (node == NULL)
            return;
        else if(node->next != NULL){
            //只给出一个要删除的结点,转移思维,改成拷贝目标结点后结点的值,然后删除目标结点的后一结点
            node->val = node->next->val;
            node->next = node->next->next;
        }
        else{
            return;
        }
    }
};

GitHub 测试程序源码

时间: 2024-10-19 00:36:54

LeetCode(237)Delete Node in a Linked List的相关文章

【LeetCode OJ 237】Delete Node in a Linked List

题目链接:https://leetcode.com/problems/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 th

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

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: 6113 Total Submissions: 12798My Submissions Question Solution 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

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

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

Cocos2d-x 3.2 学习笔记(五)Sprite Node

游戏中最重要的元素Sprite精灵,关于精灵的创建,精灵的控制等等. 涉及到的类Class: AnimationFrame 动画帧. Animation 动画对象:一个用来在精灵对象上表现动画的动画对象. AnimationCache 动画缓存单例类. 如何你想要保存动画,你需要使用这个缓存. Sprite 精灵;定义为二维图像. SpriteBatchNode 与批量节点类似,如果包含子节点会在一次OpenGL调用内绘制完成. SpriteFrame 一个精灵帧. SpriteFrameCac

[LeetCode][JavaScript]Delete Node in a Linked List

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

以太坊开发DApp实战教程——用区块链、星际文件系统(IPFS)、Node.js和MongoDB来构建电商平台(一)

第一节 简介 欢迎和我们一起来用以太坊开发构建一个去中心化电商DApp!我们将用区块链.星际文件系统(IPFS).Node.js和MongoDB来构建电商平台类似淘宝的在线电商应用,卖家可以自由地出售商品,买家可以自由地购物: 去中心化: 和淘宝或eBay不同,我们把所有的商业逻辑和核心数据都放在以太坊区块链上,这使 得它成为一个完全去中心化的应用.和淘宝这样中心化的电商平台相比,一个去中心化的P2P电商应用显然有其独特的价值--至少你不用担心被平台封账户了. IPFS: 在以太坊上存储用于商品