题意:移除链表中元素值为val的全部元素。

思路:算法复杂度肯定是O(n),那么就在追求更少代码和更少额外操作。我做不出来。

 /**
* 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) {
while(head&&head->val==val) head=head->next;
if(!head) return ;
ListNode *tmp=head;
while(head&&head->next)
{
if(head->next->val==val)
head->next=head->next->next;
else
head=head->next;
}
return tmp;
}
};

Remove Linked List Elements

LeetCode Remove Linked List Elements 删除链表元素的更多相关文章

  1. 203 Remove Linked List Elements 删除链表中的元素

    删除链表中等于给定值 val 的所有元素.示例给定: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6返回: 1 --& ...

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

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

  3. [LeetCode] Remove Linked List Elements

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

  4. LeetCode 203. Remove Linked List Elements 移除链表元素 C++/Java

    Remove all elements from a linked list of integers that have value val. Example: Input: ->->-& ...

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

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

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

  7. 【LeetCode】203. Remove Linked List Elements

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

  8. 203. Remove Linked List Elements - LeetCode

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

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

随机推荐

  1. POJ-3617

    Best Cow Line Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25616   Accepted: 6984 De ...

  2. <c和指针>学习笔记3之操作符,表达式与指针

    1 操作符 (1)移位操作符 左移<<:值最左边的几位丢弃,右边多出来的几个空位用0补齐 01101101 011(丢弃)01101000(后面三位补0) 右移>>: 算术左移 ...

  3. location对象介绍

    Location 对象 Location 对象 Location 对象包含有关当前 URL 的信息. Location 对象是 Window 对象的一个部分,可通过 window.location 属 ...

  4. 2018ACM-ICPC宁夏邀请赛 A-Maximum Element In A Stack(栈内最大值)

    Maximum Element In A Stack 20.91% 10000ms 262144K   As an ACM-ICPC newbie, Aishah is learning data s ...

  5. JDBC的初步了解及使用

    一.概念 1.什么是JDBC? JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由 ...

  6. 在Visual Studio中编译Linux的一些问题

    相对路径: 在windows下,和当前文件同一个目录下的引用会这么写: #include "SubDirectory\header.h" 或者 #include "Sub ...

  7. [Xcode 实际操作]六、媒体与动画-(7)遍历系统提供的所有滤镜

    目录:[Swift]Xcode实际操作 本文将演示系统到底提供了多少滤镜供开发者使用,并了解每个滤镜都有哪些参数需要配置. 在项目导航区,打开视图控制器的代码文件[ViewController.swi ...

  8. python 之 函数 生成器

    5.10 生成器 函数内有yield关键字,再调用函数就不会立刻执行函数体代码,会得到一个返回值,该返回值就是生成器,生成器本质就是迭代器 def chicken():    print('===== ...

  9. [转] ios数组基本用法和排序

    http://blog.csdn.net/daiyelang/article/details/18726947 1.创建数组 // 创建一个空的数组 NSArray *array = [NSArray ...

  10. MySQL server has gone away和Maximum execution time of 120 seconds exceeded

    今天在写采集时碰到两个问题1.MySQL server has gone away2.Maximum execution time of 120 seconds exceeded 采集程序写好运行大概 ...