原理:通过创建一个新的结点,放在头结点的前面,作为真正头结点的前驱结点,这样头结点就成为了意义上的非头结点,这样就可以统一操作结点的删除操作。

需要注意的是:这个新的结点是虚拟头结点,真的的头结点依然是它的后继结点,所以在C++中,我们依然需要手动删除这个虚拟头结点,返回的结点是它后面的那个结点

struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
/*
ListNode *tmp=head;
head=head->next;
delete tmp; */
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
ListNode* virtualhead = new ListNode(0);
virtualhead->next = head;
ListNode* cur = virtualhead;
while (cur->next != nullptr) {
if (cur->next->val == val) {
ListNode* t = cur->next;
cur->next = cur->next->next;
delete t;
}
else {
cur = cur->next;
}
head = virtualhead->next;
delete virtualhead;
return head;
} }
};

移除List的统一逻辑写法 LeetCode 203的更多相关文章

  1. [LeetCode] 203. Remove Linked List Elements 移除链表元素

    Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...

  2. LeetCode 203. Remove Linked List Elements (移除链表中的项)

    Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...

  3. LeetCode 203——移除链表(JAVA)

    删除链表中等于给定值 val 的所有节点. 示例: 输入: 1->2->6->3->4->5->6, val = 6 输出: 1->2->3->4 ...

  4. Java实现 LeetCode 203 移除链表元素

    203. 移除链表元素 删除链表中等于给定值 val 的所有节点. 示例: 输入: 1->2->6->3->4->5->6, val = 6 输出: 1->2 ...

  5. [LeetCode] 203. 移除链表元素(链表基本操作-删除)、876. 链表的中间结点(链表基本操作-找中间结点)

    题目 203. 移除链表元素 删除链表中等于给定值 val 的所有节点. 题解 删除结点:要注意虚拟头节点. 代码 class Solution { public ListNode removeEle ...

  6. LeetCode 203. Remove Linked List Elements 移除链表元素 C++/Java

    Remove all elements from a linked list of integers that have value val. Example: Input: ->->-& ...

  7. LeetCode 203:移除链表元素 Remove LinkedList Elements

    删除链表中等于给定值 val 的所有节点. Remove all elements from a linked list of integers that have value val. 示例: 输入 ...

  8. [LeetCode] 203. 移除链表元素

    题目链接:https://leetcode-cn.com/problems/remove-linked-list-elements/ 题目描述: 删除链表中等于给定值 val 的所有节点. 示例: 输 ...

  9. Leetcode 203 Remove Linked List Elements 链表

    去掉链表中相应的元素值 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next ...

  10. Java for LeetCode 203 Remove Linked List Elements

    Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...

随机推荐

  1. Vue+SSM+Element-Ui实现前后端分离(3)

    前言:经过博文(1)vue搭建,(2)ssm搭建,到这里就该真真切切的实现小功能了<-_-> 规划:实现登录,用户列表查询,访问日志aop; 开始先解决跨域:在目录config/index ...

  2. VMware虚拟机中Ubuntu18.04无法连接网络的有效解决办法

    对VMware虚拟机进行恢复默认网络设置 恢复虚拟网络默认设置(在断网状态下): 1)Ubuntu网络设置自动获取IP 依次单击[System Settings]–>[Network]–> ...

  3. 点击事件触发count自增

    1.vue3合成API :(即为什么要用setup() :为了数据更加关联) vue3 引入setup()合成API语法,它可以将某数据关联的内容整合到一个部分,即使setup里的内容越来越多也会围绕 ...

  4. lisp入门资料收集

    1.一些文档 http://acl.readthedocs.io/en/latest/zhCN/index.html http://daiyuwen.freeshell.org/gb/lisp.htm ...

  5. 源代码管理工具介绍(以GITHUB为例)

    Github:全球最大的社交编程及代码托管网站,可以托管各种git库,并提供一个web界面 1.基本概念 仓库(Repository):用来存放项目代码,每个项目对应一个仓库,多个开源项目则有多个仓库 ...

  6. Class 'dmstr\web\AdminLteAsset' not found

    Yii2出现  Class 'dmstr\web\AdminLteAsset' not found 报错 1.检查下是不是vendor从其他地方复制过来的 2.检查根目录composer.json 中 ...

  7. [.Net]Framwork WebAPI添加接口请求监控

    思路: 通过重写 ActionFilterAttribute 拦截Action的请求及返回信息,实现对接口请求的监听. 最终效果如下: 全局启用需配置如下: 局部启用需配置如下: 源码如下: 1 // ...

  8. 修改百分浏览器(centbrowser)、谷歌和火狐浏览器默认字体的方法

    1,百分浏览器(centbrowser) 在浏览器的安装位置D:\Program Files\Cent Browser\User Data编辑文件custome.css,如果没有此文件可新建一个,内容 ...

  9. button 样式 触发器

    <Style x:Key="Style.OkOperationButton" TargetType="ButtonBase"> <Setter ...

  10. Go_day04

    Go基础语法 指针 指针式存储另一个变量内存地址的变量 &a 取出a的内存地址 *b 若指针b存放的式a的地址 那么 *b就直接指向a的内存 可以直接操作其中的值 指针的使用 func mai ...