[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 ...
随机推荐
- Visual Assist X 10.8.2052的Crack破解补丁. 2014.11.05 (General release.)
VA小组立即更新频率似有些放缓几乎,版本号都是2个月才更新一次,而这次的更新幅度也还是比較大的,新增了6个特性,而且修正和20余处大大小小的问题,而且也是正式发行版,推荐更新. 对于破解补丁还是老规矩 ...
- GIS Tools for Hadoop 详细介绍
2013年Esri美国在开发者大会上演示了GIS数据结合Hadoop分析的一个示例,这个示例赢得了听众的阵阵掌声,我们也许对GIS不是很陌生,但是对Hadoop却不是很清楚,其实Esri是用Java开 ...
- 通过YAJL生成json语句
这里主要介绍的是怎样通过yajl生成一个json语句.方法通过代码就能够非常清楚的看到了,只是这里仅仅加入了字符串. 假设须要加入其它类型的,能够查考yajl的手冊,调用其它函数进行加入. /* * ...
- linux kickstart 自动安装
最近很多业务系统都是linux lnmp平台安装,反复的安装让人觉得很苦恼,仔细钻研了下kickstart .这里环境是red hat linux 5.8 32位,系统盘中的软件包里包含有kickst ...
- Codeforces Round #246 (Div. 2) D. Prefixes and Suffixes(后缀数组orKMP)
D. Prefixes and Suffixes time limit per test 1 second memory limit per test 256 megabytes input stan ...
- Ajax动态载入xml文件内容
<%@page import="javax.swing.JOptionPane"%> <%@page import="com.ctl.util.*&qu ...
- Linux------创建和终止进程
创建进程: Linux创建两个步骤的新处理:fork()和exec().其中fork创建当前进程的能力(父进程)副本,那个孩子.父子进程只有PID不同.在这之后,该系统具有两个进程,运行相同的操作.父 ...
- Kafka学习(一)配置及简单命令使用
一. Kafka中的相关概念的介绍 Kafka是一个scala实现的分布式消息中间件,当中涉及到的相关概念例如以下: Kafka中传递的内容称为message(消息),message 是通过topic ...
- Javascript语言精粹之String常用方法分析
Javascript语言精粹之String常用方法分析 1. String常用方法分析 1.1 String.prototype.slice() slice(start,end)方法复制string的 ...
- mysql替换字段里数据内容部分字符串(亦可用于增加字段中的内容)
mysql替换表的字段里面内容,如例子: mysql> select host,user from user where user='testuser'; +----------------- ...