[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 ...
随机推荐
- matlab矩阵的表示和简单操作
原地址:http://www.cnblogs.com/Ran_Ran/archive/2010/12/11/1903070.html 一.矩阵的表示在MATLAB中创建矩阵有以下规则: a.矩阵元素必 ...
- hdu2151(递推dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2151 分析: DP.思路:全盘扫描. i表示时间,l表示第几棵树,方程: step[i ...
- hdu1495(bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1495 题意:有三个杯子,开始时第一个杯子装满水(体积为a),倒来倒去,得到其中2个杯里的水的体积都为a ...
- Android利用反射获取状态栏(StatusBar)高度
MainActivity如下: package cc.teststatusbarheight; import java.lang.reflect.Field; import android.os.Bu ...
- 获取不同机型外置SD卡路径
/** * 运行挂载命令.返回挂载的地址.然后将地址解析 */ private void getExtSDCardPath() { try { Runtime runtime = Runtime.ge ...
- C++基础学习教程(八)
转载请注明出处:http://blog.csdn.net/suool/article/details/38300117 引入 在进行下一步的学习之前,我们须要厘清几个概念. RAII 首先介绍一个编程 ...
- Ajax—初识
看DRP的过程.又一次学习了一遍Ajax.更深刻的体会到了Ajax所具有的魅力.Ajax是一种技术或者方法,更是一 种艺术.它让我们的程序以一种更完美的姿态呈如今用户面前.以下就让我们一起走进Ajax ...
- 自定义JSTL函数标签(一)
jstl标签库的配置 * 将jstl.jar和standard.jar拷贝到WEB-INF/lib下(如果使用el表达式,不用拷贝这两个jar) 注意:jstl必须在能够支持j2ee1.4/servl ...
- 你真的了解try{ return }finally{}中的return?(转)
今天去逛论坛 时发现了一个很有趣的问题: 谁能给我我解释一下这段程序的结果为什么是:2.而不是:3 代码如下: class Test { public int aaa() { int x = 1; t ...
- sed中求公共前缀
string1="test toast" string2="test test" printf "%s\n%s\n" "$stri ...