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

题目很简单,注意链表首结点有可能更改时,需新建preHead结点,或者使用二维指针的编程方法。

 /**
* 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 head; ListNode* prehead = new ListNode();
prehead->next = head;
ListNode* curNode = prehead; while (curNode->next != NULL) {
if (curNode->next->val == val)
curNode->next = curNode->next->next;
else
curNode = curNode->next;
} head = prehead->next;
delete prehead;
return head;
}
};

【Leetcode】【Easy】Remove Linked List Elements的更多相关文章

  1. 203. Remove Linked List Elements【easy】

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

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

  3. 【LeetCode】203. Remove Linked List Elements

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

  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题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  6. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  7. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

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

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

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

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

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

随机推荐

  1. [转]C#算法题

    1:不允许使用循环语句.条件语句,在控制台中打印出1-200这200个数. 参考答案:这里我使用的是递归. static void Main(string[] args) { Print(); Con ...

  2. JMeter元件的作用域与执行顺序

    元件的作用域 先来讨论一下元件有作用域.<JMeter基础元件介绍>一节中,我们介绍了8类可被执行的元件(测试计划与线程组不属于元件),这些元件中,取样器 是典型的不与其它元件发生交互作用 ...

  3. 深入理解BSS(Block Started by Symbol)

    理解ELF的BSS section, 可以概括为: Uninitialized global/static data "Block Started by Symbol" " ...

  4. js获取当前时间(昨天、今天、明天)

    开发过程中某些前台页面的时间控件我们需要给默认当前时间,jquery可以轻松的帮我们实现,代码如下 1 //昨天的时间 2 var day1 = new Date(); 3 day1.setTime( ...

  5. Linux修改BASH命令提示符

    Shell命令提示符及颜色是由PS1来配置: 1.其中PS1常用的参数含义如下: \d :#代表日期,格式为weekday month date,例如:"Mon Aug 1" \H ...

  6. SQL 工具系列一

    1.误删除数据恢复篇 ApexSQL Recover   可以恢复Delete Truncate  drop,恢复 二进制大型对象 测试版本  每10行才会恢复 评估版本下载地址:只能用14天 所以基 ...

  7. NodeJs接口token认证express框架passport实现方式Bearer认证

    1.生成一个简单的express项目(命令:express passport-test),项目结构如下: 2.添加项目依赖: npm install passport --save npm insta ...

  8. 互联网轻量级框架SSM-查缺补漏第三天

    简言:平安夜,继续慵懒的学习.我真的是不喜欢学习··· 第三章认识MyBatis核心组件 3.1 持久层的概念和MyBatis的特点 持久层:可以将业务数据存储带磁盘,具有长期存储的能力.一般执行持久 ...

  9. hibernate5的一些坑

    SessionFactory创建的修改 如果你是刚刚从hibernate4升级到hibernate5,这时候你的项目肯定就要出错了,什么错呢? org.hibernate.MappingExcepti ...

  10. BZOJ1898: [Zjoi2005]Swamp 沼泽鳄鱼(矩阵快速幂)

    题意 题目链接 Sol 不难发现吃人鱼的运动每\(12s\)一个周期 所以暴力建12个矩阵,放在一起快速幂即可 最后余下的部分暴力乘 #include<bits/stdc++.h> usi ...