Total Accepted: 43183 Total Submissions: 160460 Difficulty: Easy

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* p = head,*next=NULL,*pre=NULL;
while(p && p->val==val){
next = p->next;
delete(p);
p = next;
head = p;
}
while(p){
if(p->val == val){
pre->next = p->next;
delete(p);
p = pre->next;
}else{
pre=p;
p=p->next;
}
}
return head;
}
};

[Linked List]Remove Linked List Elements的更多相关文章

  1. 【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 ...

  2. [LintCode] Remove Linked List Elements 移除链表元素

    Remove all elements from a linked list of integers that have value val. Have you met this question i ...

  3. Leetcode-203 Remove Linked List Elements

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

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

  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. 203. Remove Linked List Elements【easy】

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

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

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

  8. LeetCode_203. Remove Linked List Elements

    203. Remove Linked List Elements Easy Remove all elements from a linked list of integers that have v ...

  9. LeetCode--LinkedList--203. Remove Linked List Elements(Easy)

    203. Remove Linked List Elements(Easy) 题目地址https://leetcode.com/problems/remove-linked-list-elements ...

随机推荐

  1. SqlServer判断数据库、表、存储过程、函数是否存在

    假设场景是: 需要给一个脚本给客户更新, 这个对象可能存在或不存在 -- 更新存储过程 USE [数据库名] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ...

  2. 判断网络是否连接Internet

    添加 system32.Management 引用 private bool ListenNET()        {            ManagementObjectSearcher s = ...

  3. 优酷播放器demo

    <!DOCTYPE html> <html lang="en-US"> <head> <meta http-equiv="Con ...

  4. css3渐变详解

    今天总结渐变的问题,渐变分为线性渐变.径向渐变.呼呼,废话少说, 线性渐变:background:linear-gradient(设置渐变形式,第一个颜色起点,中间颜色点 中间颜色的位置,结束点颜色) ...

  5. join函数——Gevent源码分析

    在使用gevent框架的时候,我们经常会使用join函数,如下: def test1(id): print(id) gevent.sleep(0) print(id, 'is done!') t = ...

  6. ASCII码排序,hdu-2000

    Problem Description 输入三个字符后,按各字符的ASCII码从小到大的顺序输出这三个字符.   Input 输入数据有多组,每组占一行,有三个字符组成,之间无空格.   Output ...

  7. docker 使用Data Volume 共享文件

    Adding a data volume You can add a data volume to a container using the -v flag with the docker run  ...

  8. C# Chart 折线图 多条数据展示

    private void btn_Click(object sender, EventArgs e) { DBHelper db = new DBHelper(); DataSet ds = db.G ...

  9. 如何让用户在用webview访问网页时嵌入我们自己的内容

    代码如下:        NSString *strUrl=[textField text];    NSString *urlString=[NSString stringWithFormat:st ...

  10. MEMS开关

    MEMS器件在射频比如无线通信上有很好的应用.RF MEMS谐振器和诱导器品质因子在微波上有大幅度提高.MEMS开关极大地改进了高频性能和降低了能耗.本篇概要介绍MEMS开关. 自从1979年彼特森( ...