[LeetCode203]Remove Linked List Elements
题目:
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
删除一个单链表中的元素
代码:
/**
* 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) {
ListNode *preHead = new ListNode();
preHead->next = head;
ListNode *cur = head, *pre = preHead;
while(cur)
{
if(cur->val == val)
pre->next = cur->next;
else
pre = pre->next;
cur = cur->next;
}
return preHead->next;
}
};
[LeetCode203]Remove Linked List Elements的更多相关文章
- Leetcode-203 Remove Linked List Elements
#203. Remove Linked List Elements Remove all elements from a linked list of integers that have val ...
- [LintCode] Remove Linked List Elements 移除链表元素
Remove all elements from a linked list of integers that have value val. Have you met this question i ...
- 【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 ...
- 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题要求 ...
- 【LeetCode】203. Remove Linked List Elements
Remove Linked List Elements Remove all elements from a linked list of integers that have value val. ...
- 203. Remove Linked List Elements【easy】
203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...
- LeetCode Remove Linked List Elements 删除链表元素
题意:移除链表中元素值为val的全部元素. 思路:算法复杂度肯定是O(n),那么就在追求更少代码和更少额外操作.我做不出来. /** * Definition for singly-linked li ...
- LeetCode_203. Remove Linked List Elements
203. Remove Linked List Elements Easy Remove all elements from a linked list of integers that have v ...
- LeetCode--LinkedList--203. Remove Linked List Elements(Easy)
203. Remove Linked List Elements(Easy) 题目地址https://leetcode.com/problems/remove-linked-list-elements ...
随机推荐
- Linux从用户层到内核层系列 - GNU系列之glibc介绍
题记:本系列文章的目的是抛开书本从源代码和使用的角度分析Linux内核和相关源代码,byhankswang和你一起玩转linux开发 轻松搞定TCP/IP协议栈,原创文章欢迎交流, byhankswa ...
- Swift实现OC中的单例模式
一.MySingle类 import Foundation class MySingle{ //定义单例的属性 var name:String? var age:Int? var height:Dou ...
- oracle动态注冊參数local_listener
local_listener參数有两种书写格式,提供了不同的功能. 监听文件上,1521和1526port上都有动态监听port. [oracle@dbsv admin]$ cat listener. ...
- DOM中的动态NodeList与静态NodeList
GitHub版本号: https://github.com/cncounter/translation/blob/master/tiemao_2014/NodeList/NodeList.md 副标题 ...
- IntelliJ IDEA 14 注册码生成java代码(转)
https://confluence.jetbrains.com/display/IntelliJIDEA/Previous+IntelliJ+IDEA+Releases 分享几个license: ( ...
- shiro权限架作战
shiro框架作为一种特权的开源框架,通过身份验证和授权从具体的业务逻辑分离极大地提高了我们的发展速度,它的易用性使得它越来越受到人们的青睐.上一页ACL架相比,shiro能更easy的实现权限控制, ...
- maven中的java库
/* * * <dependency> <groupId>io.netty</groupId> <artifactId>n ...
- Cordova CLI源码分析(一)——简介
本系列文章分析基于node.js的命令行工具Cordova CLI,所以如果对node.js基础不是很了解,建议参考http://nodejs.gamesys.net/node-js提供的基础教程 文 ...
- Nagios+pnp4nagios+rrdtool 安装配置nagios(一)
基于的软件版本 Apache-2.0.63 php-5.3.2 nagios-3.2.3 nagios-plugins-1.4.15 rrdtool-1.4.5 nrpe-2.12 pnp4na ...
- xml publisher根据条件显示或隐藏列
xml publisher根据条件显示或隐藏列 <?if@column:condition? > -- <?end if?> 样例: 依据PROJECT_FLAG标签显示 ...