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. centos7.5安装minikube kubernetes

    前提:已安装centos7.5安装VirtualBox Minikube是什么?Minikube是一种方便在本地运行Kubernetes的工具. Minikube 是可以在VM中运行单节点的Kuber ...

  2. java reflect反射获取方法变量参数

    类的成员包含变量(Field),方法(Method),构造器(Constructor) 类定义 package Reflect; public class MyTest { public int a; ...

  3. delphi’线程新技术 并行计算

    TParallel TInterLocked 并行库中的TTask http://docwiki.embarcadero.com/Libraries/Berlin/en/System.Threadin ...

  4. WPF中TextBox文件拖放问题

    在WPF中,当我们尝试向TextBox中拖放文件,从而获取其路径时,往往无法成功(拖放文字可以成功).造成这种原因关键是WPF的TextBox对拖放事件处理机制的不同,具体可参考这篇文章Textbox ...

  5. AS3 - 对文件和目录的操作

    1,写入到文件 1 2 3 4 5 var fileObj:File = File.documentsDirectory.resolvePath("hangge.txt"); va ...

  6. APP-6-百度地图导航

    1.代码部分 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <me ...

  7. jwt-auth错误小结

    我用的是:"tymon/jwt-auth": "1.0.0-rc.1" 根据官方网站http://jwt-auth.readthedocs.io安装,并且ven ...

  8. Tcp连接的七次握手浅析

    LINUX 查看tcp连接数及状态 # netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}' TIME_WAIT 8 ...

  9. python实现查找算法:二分查找法

    二分查找算法也称折半查找,基本思想就是折半,和平时猜数字游戏一样,比如猜的数字时67,猜测范围是0-100,则会先猜测中间值50,结果小了,所以就会从50-100猜测,中间值为75,结果大了,又从50 ...

  10. ANg-梯度下降算法

    概念 为了解决线性回归问题,我们也用梯度下降算法. 算法逻辑如下: 对于线性回归模型中例子,梯度下降可以如下: 算法 实际上梯度下降可有通过求导.这里的符号":="是赋值的含义 有 ...