1. Remove Linked List Elements My Submissions QuestionEditorial Solution

    Total Accepted: 61924 Total Submissions: 215788 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

思路:先找到第一个不等于指定值的首节点,作为返回值

另外在过程中过滤掉等于val值的节点并使前面一个不为val值得节点指向下一个不为val值的节点

时间复杂度:O(n)

空间复杂度:O(1)

/**
* 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) {
if(head==NULL)return NULL;
while(head!=NULL&&head->val==val)head=head->next;//找到第一个头部
ListNode *p=head,*prep=head;
while(p!=NULL){
prep=p;
p=p->next;
while(p!=NULL&&p->val==val)p=p->next;//直到找到下一个不为val的节点并指向它
prep->next = p;
}
return head;
}
};

56-Remove Linked List Elements的更多相关文章

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

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

  2. Leetcode-203 Remove Linked List Elements

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

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

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

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

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

随机推荐

  1. 渗透测试神器——Burp的使用

    公众号:白帽子左一 版本说明:Burp Suite2.1 下载地址: 链接:https://pan.baidu.com/s/1JPV8rRjzxCL-4ubj2HVsug 提取码:zkaq 使用环境: ...

  2. 莱特币(LTC)开发环境搭建

    Linux系统下搭建莱特币LTC开发环境 1.简介 2.LTC 客户端下载 3.解压到一个固定的目录中 4.启动客户端 4.1.参数解释: 4.2.更多详细的配置 5.执行命令测试一下 6.获取莱特币 ...

  3. Python3 TypeError: initial_value must be str or None, not bytes

    response.read() returns an instance of bytes while StringIO is an in-memory stream for text only. Us ...

  4. 一条指令优化引发的血案,性能狂掉50%,clang使用-ffast-math选项后变傻了

    https://www.cnblogs.com/bbqzsl/p/15510377.html 近期在做优化时,对一些函数分别在不同编译平台上进行bench测试.发现了不少问题. 现在拿其中一个问题来分 ...

  5. p->next = q, p = q->next, q->next = p->next的区别

  6. 第三课 Dubbo设计中的设计模式

    责任链模式  责任链模式在Dubbo中发挥的作用举足轻重,就像是Dubbo框架的骨架.Dubbo的调用链组织是用责任链模式串连起来的. 责任链中的每个节点实现Filter接口,然后由ProtocolF ...

  7. 表现层(jsp)、持久层(dao)、业务层(逻辑层、service)

    转自:http://www.blogjava.net/jiabao/archive/2007/04/08/109189.html 为了实现web层(struts)和持久层(Hibernate)之间的松 ...

  8. VM的三种连接方式(转载)

    概述: VMWare提供了三种工作模式,它们是bridged(桥接模式).NAT(网络地址转换模式)和host-only(主机模式).要想在网络管理和维护中合理应用它们,你就应该先了解一下这三种工作模 ...

  9. mui轮播图为什么设置了自动播放参数也不能自动播放呢?

    最近在做项目的时候,发现Mui的轮播图遇到个问题,设置了自动播放但是怎么也不能自动播放,这是为什么呢? 原来如果动态给mui的图片轮播添加图片,又使用的ajax获取的数据,但是你ajax 还没有执行完 ...

  10. 『学了就忘』Linux软件包管理 — 47、Linux源码包的安装和卸载

    目录 1.源码包安装服务的注意事项 2.源码包安装服务的过程 3.源码包安装服务的删除 4.源码包安装服务的启动 5.源码包安装服务的关闭 1.源码包安装服务的注意事项 (1)安装服务选择哪种软件包? ...