描述

Remove all elements from a linked list of integers that have value val.

Example

Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6

Return: 1 --> 2 --> 3 --> 4 --> 5

分析

先构造一个链表结点dummyHead,并让这个结点“插到”原链表头结点之前。举个例子:假设原链表是

1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6

通过dummyHead->next=head让原链表前多了这个结点,以方便操作:

INT_MIN --> 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6

再用一个指针cur指向这个新的链表的头结点,每次做如下操作:

  • cur是否为空?若为空,退出循环,否则进入下一步;
  • cur->next是否为空?若为空,说明已遍历完链表,令cur=cur->next,退出循环;否则,继续判断cur->next->val是否等于给定的值val,若不相等,则遍历下一元素,即令cur=cur->next。若相等,说明找到该元素,进行删除结点操作。
  • 为了删除当前结点,需要用一个临时结点tmp指向该结点,然后让cur“跳过”该结点,跳过后,删除该结点,否则会造成内存泄漏。删除操作结束后,继续下一次循环。

代码如下:

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
ListNode* dummyHead = new ListNode(INT_MIN); //create a node as "new head"
dummyHead -> next = head;
ListNode* cur = dummyHead;
while(cur){
if(cur -> next && cur -> next -> val == val){ //be sure of that cur -> next is valid
ListNode* tmp = cur -> next; //store the node we want to delete
cur -> next = tmp -> next;
delete tmp; //delete the node or it may cause memory leak
}else
cur = cur -> next; //otherwise modify cur
}
return dummyHead -> next;
}
};

leetcode解题报告(28):Remove Linked List Elements的更多相关文章

  1. LeetCode解题报告—— 4Sum & Remove Nth Node From End of List & Generate Parentheses

    1. 4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + ...

  2. 【leetcode❤python】 203. Remove Linked List Elements

    #-*- coding: UTF-8 -*- # Definition for singly-linked list.# class ListNode(object):#     def __init ...

  3. leetcode解题报告(2):Remove Duplicates from Sorted ArrayII

    描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  4. LeetCode解题报告:Linked List Cycle && Linked List Cycle II

    LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...

  5. 【LeetCode】237 & 203 - Delete Node in a Linked List & Remove Linked List Elements

    237 - Delete Node in a Linked List Write a function to delete a node (except the tail) in a singly l ...

  6. leetcode 203. Remove Linked List Elements 、83. Remove Duplicates from Sorted List 、82. Remove Duplicates from Sorted List II(剑指offer57 删除链表中重复的结点)

    203题是在链表中删除一个固定的值,83题是在链表中删除重复的数值,但要保留一个:82也是删除重复的数值,但重复的都删除,不保留. 比如[1.2.2.3],83题要求的结果是[1.2.3],82题要求 ...

  7. 【LeetCode】203. Remove Linked List Elements

    Remove Linked List Elements Remove all elements from a linked list of integers that have value val. ...

  8. LeetCode Remove Linked List Elements 删除链表元素

    题意:移除链表中元素值为val的全部元素. 思路:算法复杂度肯定是O(n),那么就在追求更少代码和更少额外操作.我做不出来. /** * Definition for singly-linked li ...

  9. 【刷题-LeetCode】203. Remove Linked List Elements

    Remove Linked List Elements Remove all elements from a linked list of integers that have value *val* ...

  10. 203. Remove Linked List Elements - LeetCode

    Question 203. Remove Linked List Elements Solution 题目大意:从链表中删除给定的数 思路:遍历链表,如果该节点的值等于给的数就删除该节点,注意首节点 ...

随机推荐

  1. jedis参数不当引发的问题总结

    jedis参数不当引发dubbo服务线程池耗尽异常 现象:一个dubbo服务偶发性的出现个别机器甚至整个集群大量报线程池耗尽的问题.一开始对问题的处理比较粗暴,直接增加了10倍的线程数.但是问题依然偶 ...

  2. 启动Spring boot项目报错:java.lang.IllegalArgumentException: LoggerFactory is not a Logback

    java.lang.IllegalArgumentException: LoggerFactory is not a Logback LoggerContext but Logback is on t ...

  3. shell-基础2-字符串文本处理${}

    一.为什么使用${}引用变量 1.$a和${a}的效果与区别 因为个别特殊字符会影响正常引用,所以需要使用${}引用变量,加花括号是为了帮助解释器识别变量的边界 $a和${a}效果一样,当变量后面连接 ...

  4. 在 WPF 中获取一个依赖对象的所有依赖项属性

    原文:在 WPF 中获取一个依赖对象的所有依赖项属性 本文介绍如何在 WPF 中获取一个依赖对象的所有依赖项属性. 本文内容 通过 WPF 标记获取 通过设计器专用方法获取 通过 WPF 标记获取 p ...

  5. Unity - Profiler参数详解

    CPU Usage ​       ● GC Alloc - 记录了游戏运行时代码产生的堆内存分配.这会导致ManagedHeap增大,加速GC的到来.我们要尽可能避免不必要的堆内存分配,同时注意:1 ...

  6. ToLua Timer机制

    从一个Bug说起: 在内部试玩时发现有个任务的玩家跟随Npc逻辑挂了. telnet连接到出问题的设备上, 开始搞事情 这个跟随的逻辑是一个Timer驱动的. 这个Timer在主角创建时就会启动. 一 ...

  7. html 滚动条样式

    转载:https://www.cnblogs.com/yclblog/p/6806496.html /*定义滚动条高宽及背景 高宽分别对应横竖滚动条的尺寸*/ ::-webkit-scrollbar ...

  8. Mac 修改hostname

    mac终端上输入命令: sudo scutil --set HostName newname # 实例 sudo scutil --set HostName master

  9. git如何删除已经提交的文件夹

    在上传项目到github时,忘记忽略了某个文件夹.idea,就直接push上去了, 最后意识到了此问题,决定删除掉远程仓库中的.idea文件夹 删除前: 删除后: 在github上只能删除仓库,却无法 ...

  10. ASP.NET Core 2.2 项目升级至 3.0 备忘录

    将 ASP.NET Core 2.2 迁移至 ASP.NET Core 3.0 需要注意的地方记录在这篇随笔中. TargetFramework 改为 netcoreapp3.0 <Target ...