LeetCode Remove Linked List Elements 删除链表元素

题意:移除链表中元素值为val的全部元素。
思路:算法复杂度肯定是O(n),那么就在追求更少代码和更少额外操作。我做不出来。
/**
* 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) {
while(head&&head->val==val) head=head->next;
if(!head) return ;
ListNode *tmp=head;
while(head&&head->next)
{
if(head->next->val==val)
head->next=head->next->next;
else
head=head->next;
}
return tmp;
}
};
Remove Linked List Elements
LeetCode Remove Linked List Elements 删除链表元素的更多相关文章
- 203 Remove Linked List Elements 删除链表中的元素
删除链表中等于给定值 val 的所有元素.示例给定: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6返回: 1 --& ...
- [LeetCode] Remove Linked List Elements 移除链表元素
Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...
- [LeetCode] Remove Linked List Elements
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- LeetCode 203. Remove Linked List Elements 移除链表元素 C++/Java
Remove all elements from a linked list of integers that have value val. Example: Input: ->->-& ...
- 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题要求 ...
- [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】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 - LeetCode
Question 203. Remove Linked List Elements Solution 题目大意:从链表中删除给定的数 思路:遍历链表,如果该节点的值等于给的数就删除该节点,注意首节点 ...
- 【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 ...
随机推荐
- 表单enctype属性
首先知道enctype这个属性管理的是表单的MIME编码.共有三个值可选:1.application/x-www-form-urlencoded2.multipart/form-data3.text/ ...
- python :字符串,列表,元组,集合,字典
字符串方法: 字符串是一个有序的,不可修改的,元素是以引号包围的序列.单引号,双引号,三引号,str生成 字符串的修饰 a='novo gene' a.center() 让字符串在指定的长度居中显 ...
- 使用BIND安装智能DNS服务器(二)---配置rndc远程控制
首先两个BIND DNS服务器要正常运行. 主DNS服务器IP:192.168.1.100 客户机DNS服务器IP:192.168.1.101 1 主DNS端配置: cd /etc/ 生成 ...
- 自定义TabWidget
在开发过程中,默认的TabWidget不能满足我们对于UI的要求并且没有足够的属性工我们去修改,这个时候能够自定义TabWidget是非常必要的.自定义TabWidget组要运用的是TabSpec.s ...
- display:block inline-block inlined的区别
一.首先要了解什么是块级元素与行级元素 块级元素 会占领页面的一行,其后多个block元素自动换行. 可以设置width,height,设置了width后同样也占领一行.同样也可以设置 margi ...
- poj3667(线段树区间合并&区间查询)
题目链接: http://poj.org/problem?id=3667 题意:第一行输入 n, m表示有 n 间房间(连成一排的), 接下来有 m 行输入, 对于接下来的 m 行输入: 1 x : ...
- 道阻且长 莫失本心(我的OI之旅
道阻且长 莫失本心 今天午睡醒来,发现自己发烧了.尽管穿的很厚,尽管今天的阳光如此明媚.身体还是由内而外地散发着冷意. 期末考已经结束了,接下来又是艰难而难忘的奥赛训练.(要到春节才能放假了 ...
- SpringSecurity为项目加入权限控制
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- 一个线性表中的元素为整数,设计一个算法,将正整数和负整数分开,使线性表的前一半为负整数,后一半为正整数。(C语言)
以下为完整可运行示例代码: #include <stdio.h> #include <stdlib.h> typedef struct LNode{ int data; str ...
- 题解 P1006 传纸条
传送门 其实我觉得这个跟P1004挺类似(又是动规) 题解P1004 #include<iostream> #include<cstdio> #include<cstri ...