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. 详解Vue中的nextTick

    Vue中的nextTick涉及到Vue中DOM的异步更新,感觉很有意思,特意了解了一下.其中关于nextTick的源码涉及到不少知识,很多不太理解,暂且根据自己的一些感悟介绍下nextTick. 一. ...

  2. impala 导出CSV 或excel

    1.介绍 impala-shell导入导出 参数说明: • -q query (--query=query) 从命令行执行查询,不进入impala-shell • -d default_db (--d ...

  3. Android -- EventBus解析

    EventBus EventBus 是一个 Android 事件发布/订阅框架,通过解耦发布者和订阅者简化 Android 事件传递.传统的事件传递方式包括:Handler.BroadCastRece ...

  4. SCIKIT-LEARN与GBDT使用案例

    http://blog.csdn.net/superzrx/article/details/47073847 安装 SCIKIT-LEARN是一个基于Python/numpy/scipy的机器学习库  ...

  5. jquery事件使用方法总结 (转)

    http://www.cnblogs.com/cwp-bg/p/7668940.html jquery提供了许多的事件处理函数,学习前端一段时间了,下面对其总结一下,梳理一下知识点. 一.鼠标事件 1 ...

  6. mysqli

    CREATE TABLE `user` ( `id` ) NOT NULL, `name` ) NOT NULL, `password` ) NOT NULL ) ENGINE=InnoDB DEFA ...

  7. Masonry自动布局使用

    Masonry是一个轻量级的布局框架,采用更好的语法封装自动布局,它有自己的布局DSL.简洁明了并具有高可读性 而且同时支持 iOS 和 Max OS X. 下载 NSLayoutConstraint ...

  8. 虚拟机Linux下解决ping时出现 unknown host问题

    在虚拟机中使用CentOS6.5时,ping www.baidu.com出现报错信息:“ping: unknown hostwww.baidu.com”,虚拟机和物理机网络连接是NAT方式,物理机访问 ...

  9. ASP输出JSON数据及客户端jQuery处理方法

    首先ASP处理JSON需要json官方提供的JSON For ASP 封装类文件,下载地址:http://code.google.com/p/aspjson/downloads/list 下载最新的J ...

  10. guava 学习笔记 瓜娃(guava)的API快速熟悉使用

    1,大纲 让我们来熟悉瓜娃,并体验下它的一些API,分成如下几个部分: Introduction Guava Collection API Guava Basic Utilities IO API C ...