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

/**
* 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){
if(cur->val == val){
pre->next = cur->next;
}
else{
pre = pre->next;
}
cur = cur->next;
}
return newHead->next;
}
};

203. Remove Linked List Elements (List)的更多相关文章

  1. 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题要求 ...

  2. 203. Remove Linked List Elements【easy】

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

  3. 203. Remove Linked List Elements - LeetCode

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

  4. 【LeetCode】203. Remove Linked List Elements

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

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

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

  6. 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 -- ...

  7. 203. Remove Linked List Elements

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

  8. (easy)LeetCode 203.Remove Linked List Elements

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

  9. Java [Leetcode 203]Remove Linked List Elements

    题目描述: Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> ...

  10. [LeetCode] 203. Remove Linked List Elements 解题思路

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

随机推荐

  1. springMVC数据模型model,modelmap,map,@ModelAttribute的相互关系

    结论: a.注解方法中形参为model,modelmap,map一个或几个时,他们指向的引用对象相同即他们的值相同. b.当使用@ModelAttribute注解请求参数时,springmvc自动将该 ...

  2. System.Types.hpp(77): E2029 'TObject' must be a previously defined class or struct

    System.Types.hpp System.Types.hpp(77): E2029 'TObject' must be a previously defined class or struct ...

  3. php & 变量引用、函数引用、对象引用

    变量的引用        PHP 的引用允许你用两个变量来指向同一个内容 <?php $a="ABC"; $b =&$a; echo $a;//这里输出:ABC ec ...

  4. 机器学习进阶-案例实战-停车场车位识别-keras预测是否停车站有车

    import numpy import os from keras import applications from keras.preprocessing.image import ImageDat ...

  5. APP-3-百度地图应用

    1.百度地图开发平台 http://lbsyun.baidu.com/ 1.1申请账号 1.2Android创建应用 进入百度地图开发平台->控制台->创建应用 发布版SHA1:BA:AD ...

  6. 如何做Go的性能优化?(转)

    Go的性能优化其实总的来说和C/C++等这些都差不多,但也有它自己独有的排查方法和陷阱,这些都来源于它的语言特性和环境. 1.性能优化前提——任何好的东西都是在正确的前提上 代码界的很多事是和我们生活 ...

  7. 3.有关于Python列表简述

    一..title() [让所选择的列表元素的第一个字母大写] test = ['no1','No2','No3','No4'] book = "This My " + test[0 ...

  8. debian下redis2.8.17安装过程

    下载redis源码包,我下载的是redis2.8.17 解压缩该源码包 tar zxf redis-2.8.17.tar.gz 进入解压缩后的目录 cd redis-2.8.17/ 添加redis用户 ...

  9. Windows查看指定端口是否占用和查看进程

    Winodows上查看指定端口号的使用情况和占用进程以及终止所占用端口进程进程用到.下面主要描述如何操作. 1.查看所有端口占用情况 C:\Users\Administrator>netstat ...

  10. JDBC有哪些接口

    1 实现Driver接口的对象是JDBC进行数据库访问的开始,可以通过java.lang.Class类的forName(),动态加载驱动程序. Class.forName("驱动程序&quo ...