[LeetCode]237. Delete Node in a Linked List

限时加码!20+主流AI编程工具免费用 购周边加赠Coding Plan Lite,Claude Code、Cursor等即刻畅享,学习进阶更高效! 阅读详情

[LeetCode]237. Delete Node in a Linked List

题目描述

这里写图片描述

思路

安全性考虑,删除临时变量

代码

#include <iostream>

using namespace std;

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

    void printList(ListNode* node) {
        while (node){
            cout << node->val << " ";
            node = node->next;
        }
        cout << endl;
    }
};

int main() {
    ListNode *n1 = new ListNode(1);
    ListNode *n2 = new ListNode(2);
    ListNode *n3 = new ListNode(3);
    ListNode *n4 = new ListNode(4);

    n1->next = n2;
    n2->next = n3;
    n3->next = n4;

    Solution s;
    s.printList(n1);
    s.deleteNode(n3);
    s.printList(n1);

    system("pause");

}
leetcode 237: Delete Node in a Linked List leetcode 237: Delete Node in a Linked List PYTHON JAVA C++ 阅读详情

相关推荐

Leetcode 237 Delete node in a Linked List

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. Given linked list – head = [4,5,1,9], which...

qq_43271202的博客 180

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

哈哈的个人专栏 6949

Leetcode 237. Delete Node in a Linked List

【代码】Leetcode 237. Delete Node in a Linked List

小白菜又菜 587

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. Given linked list -- head = [4,5,1,9], which looks like following: ...

乌鲁木齐001号程序员 153

Leetcode_237_Delete Node in a Linked List

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/47334649 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

皮斯特劳沃 1902

leetcode 237. Delete Node in a Linked List

237. Delete Node in a Linked List   Add to List Question Editorial Solution  My Submissions Total Accepted: 125969Total Submissions: 277762Difficulty: EasyContributors:

怪盗基德 392

[leetcode] 237. Delete Node in a Linked List

Description Write a function to delete a node in a singly-linked list. You will not be given access to the head of the list, instead you will be given access to the node to be deleted directly. It is guaranteed that the node to be deleted is not a tail nod

Learning from the mistakes 178

LeetCode 237. Delete Node in a Linked List 解题报告

LeetCode 237. Delete Node in a Linked List 解题报告

骆小坑的填坑之旅 507

leetcode237 Delete Node in a Linked List

237. Delete Node in a Linked List Total Accepted: 66681 Total Submissions: 152654 Difficulty: Easy Write a function to delete a node (except the tail) in a singly linked list, given on

葡萄架的程序猿 347

leetcode237. 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. Given linked list – head = [4,5,1,9], which looks like following: Example 1: Input: he...

中华小当家的博客 177

[LeetCode] 237. Delete Node in a Linked List

原题链接: https://leetcode.com/problems/delete-node-in-a-linked-list/ 1. 题目介绍 Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Given linked list...

Ber03的博客 194

[leetcode] 237. Delete Node in a Linked List @ python

原题 Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Given linked list – head = [4,5,1,9], which looks like following: Example 1: Input: head...

闲庭信步 209

leetcode 237 Delete Node in a Linked List

题目链接:https://leetcode.com/problems/delete-node-in-a-linked-list/ 思路分析:题目要求在链表中删除给定的node,该node不为链表中的tail。因为没有node的前一个结点,所以想要直接从linked list中直接删除结点node是不可能的。 根据题目信息,可以通过将node.next结点的值赋给node,并删除nod...

aqjh9999的博客 160

LeetCode237. 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. Given linked list --head =[4,5,1,9], which looks like following: Example 1: Inpu...

李歇特冯-兹拜因巴哈的博客 194

leetcode237. Delete Node in a Linked List

problem 237. Delete Node in a Linked List 这道题是删除链表的一个节点,和通常情况不同的是,没有给出链表的起点,只给了一个要删的节点。一般来说删除一个节点的方法是要有其前一个节点的位置,然后将其前一个节点的next连向要删节点的下一个,然后delete掉要删的节点即可。这道题的处理方法是先把当前节点的值用下一个节点的值覆盖了,然后我们删除下一个节点即可...

weixin_30576859的博客 105

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. Given linked list – head = [4,5,1,9] , which looks like following: Example 1: Input: head = [4,5,1,9], node = 5 Output: [4,1,9] Explanation: You a

memcpy0的博客 606

leetcode 237. Delete Node in a Linked List 解法 python

.问题描述 Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Given linked list --head =[4,5,1,9], which looks like following: Example 1...

CSerwangjun的博客 417
上一篇: [LeetCode]242. Valid Anagram
下一篇: [LeetCode]217. Contains Duplicate
charon____
博客等级 码龄11年 3粉丝 202原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值