Remove Linked List Elements

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

Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.

遇到val则删除,否则继续前进,直到链表结尾。

/**
* 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* newhead = new ListNode(-);
newhead->next = head;
ListNode* pre = newhead;
ListNode* cur = head;
while(cur != NULL)
{
if(cur->val == val)
{
cur = cur->next;
pre->next = cur;
}
else
{
pre = cur;
cur = cur->next;
}
}
return newhead->next;
}
};

【LeetCode】203. Remove Linked List Elements的更多相关文章

  1. 【LeetCode】203. Remove Linked List Elements 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 递归 日期 题目地址:https://lee ...

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

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

  3. 【一天一道Leetcode】#203.Remove Linked List Elements

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 我的个人博客已创建,欢迎大家持续关注! 一天一道le ...

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

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

  5. 203. Remove Linked List Elements【easy】

    203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...

  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. 203. Remove Linked List Elements - LeetCode

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

  8. 【LeetCode】402. Remove K Digits 解题报告(Python)

    [LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

  9. 【LeetCode】722. Remove Comments 解题报告(Python)

    [LeetCode]722. Remove Comments 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/remove-c ...

随机推荐

  1. windows无法访问vmware搭建好虚拟机linux web服务器

    [前置条件] vmware搭建好虚拟机web服务器 ,但是本机就是无法访问的解决办法. linux虚拟机的网络选择Bridged 桥接到本机网卡. 具体情况如下 : 1.本机能ping通虚拟机 2.虚 ...

  2. linux邮件系统的优势和便利性

    国内知名企业邮箱系统品牌商U-Mail张工在接受有关媒体采访时,特别推荐Linux版本的邮件系统.有利于与移动平台整合在Linux的U-Mail邮件服务器软件后台添加了微信版管理模块,可以查看列表,而 ...

  3. GIT 如何从另一分支合并特定的文件

    是否遇到过这种情景: 您在一个分支上工作,发现该分支上的某些文件实现的功能已经在其他分支上实现了 但因为这两个分支实现不同的功能,因此不能进行简单的合并工作,但您又不想重复其他已经完成的工作 以下操作 ...

  4. Eclipse Maven项目报错3之找不到配置文件spring-servlet-context.xml

    一.具体错误如下图所示 根据文字提示可以看出是这个文件找不到,但是我去项目的这个目录找了,这个文件确实存在,那么是什么问题呢 二.解决问题 原因分析(来自网上) 代码编译的过程,是一个自动生成相应编译 ...

  5. python绘制很美丽的图表

    或许你会觉得python不适合做图形界面的开发,的确如此.可是python却有一个非常美丽的图标模块:pycha,废话少说,先上图,各位看一下. 是不是效果还不错呢,当然这仅仅是一小部分图表,还有其它 ...

  6. Oracle数据库imp

    创建表空间 create tablespace ICITY datafile 'D:\Oracle\oradata\orcl\ICITY.DBF' size 400M autoextend on ne ...

  7. 使用Newtonsoft进行JSON序列化时将枚举序列化为字符串的方法

    一.实体书写 将枚举类型的属性前面加上[JsonConverter(typeof(StringEnumConverter))]即可. 二.举例 [JsonConverter(typeof(String ...

  8. Unbuntu和Centos中部署同时多版本PHP的详细过程

        镜像制作:Unbuntu14 部署LAMP过程 1.Azure经典版中创建源Ubuntu14,并使用Xshell连接,并切换到root帐户下. 2.安装php5.4,新建/var/local/ ...

  9. shell alias添加别名使用

    大家一定知道SHELL的基本用法,那么著名的命令:`ll`是代表`ls -l`,那么是怎么实现的哪?其实是添加了一个别名alias ll="ls -l" 我使用alias最多的地方 ...

  10. MapReduce任务参数调优(转)

    http://blog.javachen.com/2014/06/24/tuning-in-mapreduce/ 本文主要记录Hadoop 2.x版本中MapReduce参数调优,不涉及Yarn的调优 ...