​LeetCode刷题实战237:删除链表中的节点

沙海 2021年4月15日01:42:18杂谈 Java评论28字数 1564阅读5分12秒阅读模式
摘要

速读摘要

速读摘要文章源自JAVA秀-https://www.javaxiu.com/12460.html

请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点。给定的节点为非末尾节点并且一定是链表中的一个有效节点。与下一个节点交换从链表里删除一个节点node的最常见方法是修改之前节点的next指针,使其指向之后的节点。文章源自JAVA秀-https://www.javaxiu.com/12460.html

原文约 2486 | 图片 6 | 建议阅读 5 分钟 | 评价反馈文章源自JAVA秀-https://www.javaxiu.com/12460.html

​LeetCode刷题实战237:删除链表中的节点

原创 小猿同学 程序IT圈 文章源自JAVA秀-https://www.javaxiu.com/12460.html

收录于话题文章源自JAVA秀-https://www.javaxiu.com/12460.html

#Leecode刷题文章源自JAVA秀-https://www.javaxiu.com/12460.html

111个文章源自JAVA秀-https://www.javaxiu.com/12460.html

算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !文章源自JAVA秀-https://www.javaxiu.com/12460.html

文章源自JAVA秀-https://www.javaxiu.com/12460.html

今天和大家聊的问题叫做 删除链表中的节点,我们先来看题面:文章源自JAVA秀-https://www.javaxiu.com/12460.html

https://leetcode-cn.com/problems/delete-node-in-a-linked-list/文章源自JAVA秀-https://www.javaxiu.com/12460.html

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.文章源自JAVA秀-https://www.javaxiu.com/12460.html

请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点。传入函数的唯一参数为 要被删除的节点 。文章源自JAVA秀-https://www.javaxiu.com/12460.html

​LeetCode刷题实战237:删除链表中的节点文章源自JAVA秀-https://www.javaxiu.com/12460.html

示例

文章源自JAVA秀-https://www.javaxiu.com/12460.html

示例 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.提示:链表至少包含两个节点。链表中所有节点的值都是唯一的。给定的节点为非末尾节点并且一定是链表中的一个有效节点。不要从你的函数中返回任何结果。
文章源自JAVA秀-https://www.javaxiu.com/12460.html

解题

与下一个节点交换文章源自JAVA秀-https://www.javaxiu.com/12460.html

从链表里删除一个节点node的最常见方法是修改之前节点的next指针,使其指向之后的节点。文章源自JAVA秀-https://www.javaxiu.com/12460.html

​LeetCode刷题实战237:删除链表中的节点文章源自JAVA秀-https://www.javaxiu.com/12460.html

因为,我们无法访问我们想要删除的节点 之前 的节点,我们始终不能修改该节点的 next 指针。相反,我们必须将想要删除的节点的值替换为它后面节点中的值,然后删除它之后的节点。文章源自JAVA秀-https://www.javaxiu.com/12460.html

​LeetCode刷题实战237:删除链表中的节点文章源自JAVA秀-https://www.javaxiu.com/12460.html

​LeetCode刷题实战237:删除链表中的节点文章源自JAVA秀-https://www.javaxiu.com/12460.html

​LeetCode刷题实战237:删除链表中的节点文章源自JAVA秀-https://www.javaxiu.com/12460.html

因为我们知道要删除的节点不是列表的末尾,所以我们可以保证这种方法是可行的。文章源自JAVA秀-https://www.javaxiu.com/12460.html

class Solution {public:    void deleteNode(ListNode* node) {        node->val = node->next->val;        node->next = node->next->next;    }};
文章源自JAVA秀-https://www.javaxiu.com/12460.html

好了,今天的文章就到这里,如果觉得有所收获,请顺手点个在看或者转发吧,你们的支持是我最大的动力 。文章源自JAVA秀-https://www.javaxiu.com/12460.html

上期推文:文章源自JAVA秀-https://www.javaxiu.com/12460.html

LeetCode1-220题汇总,希望对你有点帮助!文章源自JAVA秀-https://www.javaxiu.com/12460.html

LeetCode刷题实战221:最大正方形文章源自JAVA秀-https://www.javaxiu.com/12460.html

LeetCode刷题实战222:完全二叉树的节点个数文章源自JAVA秀-https://www.javaxiu.com/12460.html

LeetCode刷题实战223:矩形面积文章源自JAVA秀-https://www.javaxiu.com/12460.html

LeetCode刷题实战224:基本计算器文章源自JAVA秀-https://www.javaxiu.com/12460.html

LeetCode刷题实战225:用队列实现栈文章源自JAVA秀-https://www.javaxiu.com/12460.html

LeetCode刷题实战226:翻转二叉树文章源自JAVA秀-https://www.javaxiu.com/12460.html

LeetCode刷题实战227:基本计算器 II文章源自JAVA秀-https://www.javaxiu.com/12460.html

LeetCode刷题实战228:汇总区间文章源自JAVA秀-https://www.javaxiu.com/12460.html

LeetCode刷题实战229:求众数 II文章源自JAVA秀-https://www.javaxiu.com/12460.html

LeetCode刷题实战230:二叉搜索树中第K小的元素文章源自JAVA秀-https://www.javaxiu.com/12460.html

LeetCode刷题实战231:2的幂文章源自JAVA秀-https://www.javaxiu.com/12460.html

LeetCode刷题实战232:用栈实现队列文章源自JAVA秀-https://www.javaxiu.com/12460.html

LeetCode刷题实战233:数字 1 的个数文章源自JAVA秀-https://www.javaxiu.com/12460.html

LeetCode刷题实战234:回文链表文章源自JAVA秀-https://www.javaxiu.com/12460.html

LeetCode刷题实战235:二叉搜索树的最近公共祖先文章源自JAVA秀-https://www.javaxiu.com/12460.html

LeetCode刷题实战236:二叉树的最近公共祖先文章源自JAVA秀-https://www.javaxiu.com/12460.html

​LeetCode刷题实战237:删除链表中的节点文章源自JAVA秀-https://www.javaxiu.com/12460.html

继续阅读
速蛙云 - 极致体验,强烈推荐!!!购买套餐就免费送各大视频网站会员!快速稳定、独家福利社、流媒体稳定解锁!速度快,全球上网、视频、游戏加速、独立IP均支持!基础套餐性价比很高!这里不多说,我一直正在使用,推荐购买:https://www.javaxiu.com/59919.html
weinxin
资源分享QQ群
本站是JAVA秀团队的技术分享社区, 会经常分享资源和教程; 分享的时代, 请别再沉默!
沙海
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定