题意:移除链表中元素值为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. 《精通Spring4.X企业应用开发实战》读后感第五章(Bean作用域)

  2. CodeForces 489C Given Length and Sum of Digits... (dfs)

    C. Given Length and Sum of Digits... time limit per test 1 second memory limit per test 256 megabyte ...

  3. linux c二级指针的内存分配和使用

    #include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h> ...

  4. 通过xpath获取对应的整个子节点信息

    1.页面解析接口 package com.dajiangtai.djt_spider.service; import com.dajiangtai.djt_spider.entity.Page; /* ...

  5. 手机端处理布局rem

    方法一 if (document.documentElement.clientWidth > 600) {//页面宽度大于600px让其宽度等于600px,字体大小等于60px,居中 docum ...

  6. RVO算法

    http://blog.sina.com.cn/s/blog_6ad33d350102xqal.html 简介 在介绍VO,RVO之前,需要先介绍路径规划. 对Agent进行路径规划,实际上要完成的任 ...

  7. 洛谷P2759 奇怪的函数

    P2759 奇怪的函数 题目描述 使得 x^x 达到或超过 n 位数字的最小正整数 x 是多少? 输入输出格式 输入格式: 一个正整数 n 输出格式: 使得 x^x 达到 n 位数字的最小正整数 x ...

  8. iOS 根据文字字数动态确定Label宽高

    iOS7中用以下方法 - (CGSize)sizeWithAttributes:(NSDictionary *)attrs; 替代过时的iOS6中的- (CGSize)sizeWithFont:(UI ...

  9. require--按需加载js与模块化

    1.html中:  “<script type="text/javascript" src="js/require.js" data-main=" ...

  10. Unity 打包PC和安卓的路径注意事项

    if UNITY_STANDALONE_WIN || UNITY_EDITOR return Application.persistentDataPath + "/LocalData&quo ...