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. 2016424王启元 Exp5 msf基础应用

    成功实现的有: 一个主动攻击实践,ms08_067 一个针对浏览器的攻击,ms11_050 一个针对客户端的攻击,Adobe(在实现这一步骤的时候有点小瑕疵,就是Adobe的版本问题换了三四个才成功) ...

  2. C#常用的引用

    1.使用ConfigurationManager需要在.net引用中添加System.Configuration引用 2.使用HttpContext需要在.net引用中添加System.Web引用

  3. (转)linux内存源码分析 - 内存回收(整体流程)

    http://www.cnblogs.com/tolimit/p/5435068.html------------linux内存源码分析 - 内存回收(整体流程) 概述 当linux系统内存压力就大时 ...

  4. sql 查询目标数据库中所有的表以其关键信息

    1.查询目标库中的所有表 SELECT obj.name tablename, ---表名 schem.name schemname, ---表所属的方案 idx.rows, ---一共有几行数组 C ...

  5. 关于Jquery事件绑定的心得

    今日在工作的时候,遇上了一些和事件绑定有关的问题无法解决,于是在网上找到了以下资料,稍加整理了下. 对于事件绑定,jQuery的 bind / unbind 大多数时候可能并不会用到,取而代之的是直接 ...

  6. PyCharm鼠标右键不显示Run unittest方法

    PyCharm鼠标右键不显示Run unittest方法 PyCharm是一个用来写python代码的IDE,很好用.在其中建立了unittest类后,鼠标点击某个test方法后,菜单中会显示Run ...

  7. 有向图和无向图的数组C++实现

    源码:https://github.com/cjy513203427/C_Program_Base/tree/master/55.%E5%9B%BE 结点类Noded.h 不需要存储索引 #pragm ...

  8. Access入门 2010,数学

    Access入门 2010(高级窗体) 1,创建数据透视图:创建---其他窗体---数据透视图---图标字段列表---选择列表内容---移动到数据透视图的对应位置---完成. 2,创建数据透视表窗体: ...

  9. 06.密封类 sealed

    sealed   修饰 密封类是不能被继承的. 密封类是可以去继承别的类. namespace _07.密封类 { class Program { static void Main(string[] ...

  10. Java API 之 Properties 类

    1.简介 在项目中我们经常看到一种格式极其干净的配置文件,如:config.properties.在Java的体系结构中也提供了API对properties文件进行读取和写入等操作,即:Propert ...