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

需要注意的是:这个新的结点是虚拟头结点,真的的头结点依然是它的后继结点,所以在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. Unity 导出设置iOS 项目

    别人的代码 xcode打包部分设置的脚本如下 public class XcodeSetting : MonoBehaviour { private static List<Menu> m ...

  2. 实验5 开源控制器实践——POX

    实验5 开源控制器实践--POX 一.实验目的 1.能够理解 POX 控制器的工作原理: 2.通过验证POX的forwarding.hub和forwarding.l2_learning模块,初步掌握P ...

  3. LNMP架构的演变

    LNMP 演变 工作原理 linux + nginx + mariadb + php 工作原理: 首先,浏览器发送http request请求到服务器(Nginx),服务器响应并处理web请求, 将一 ...

  4. 线上Java调优-Arthas入门

    1.SSH连接目标主机,找到对应容器ID docker ps | grep eam 2.进入容器,并启用bash docker exec -it 01c6ab243ff4 /bin/bash 3.按A ...

  5. decode procedure

    1  test data preparation 1>  select representative data voice to match real application scenario ...

  6. c/s winForm框架 tabpage标签切换窗体

    /// <summary> /// 根据窗体Name打开窗体 /// </summary> /// <param name="name">< ...

  7. 【原创】2022年linux环境下QT6不支持中文输入法解决方案

    1.配置环境 export PATH="~/目录/Qt/6.x.x/gcc_64/bin":$PATH export PATH="~/目录/Qt/Tools/Cmake/ ...

  8. jedis使用分布式锁

    import redis.clients.jedis.Jedis;public class A { public static void main(String[] args) throws Exce ...

  9. django生命周期流程以及无有名分组和反向解析 JsonResponse和form表单上传

    django的请求生命周期流程图 要求每个人必须会画,帮助你梳理django的大致流程 路由层 1. 路由匹配:urls.py 这个文件是django框架的总路由文件,意味着还有分路由文件,每个应用都 ...

  10. 简单总结一下html中能见到的各种相对路径

    试列举如下(在本文中,星号表示资源名): href="/*" href="//*" href="*" href="./*" ...