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 ...
随机推荐
- 《精通Spring4.X企业应用开发实战》读后感第五章(Bean作用域)
- CodeForces 489C Given Length and Sum of Digits... (dfs)
C. Given Length and Sum of Digits... time limit per test 1 second memory limit per test 256 megabyte ...
- linux c二级指针的内存分配和使用
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h> ...
- 通过xpath获取对应的整个子节点信息
1.页面解析接口 package com.dajiangtai.djt_spider.service; import com.dajiangtai.djt_spider.entity.Page; /* ...
- 手机端处理布局rem
方法一 if (document.documentElement.clientWidth > 600) {//页面宽度大于600px让其宽度等于600px,字体大小等于60px,居中 docum ...
- RVO算法
http://blog.sina.com.cn/s/blog_6ad33d350102xqal.html 简介 在介绍VO,RVO之前,需要先介绍路径规划. 对Agent进行路径规划,实际上要完成的任 ...
- 洛谷P2759 奇怪的函数
P2759 奇怪的函数 题目描述 使得 x^x 达到或超过 n 位数字的最小正整数 x 是多少? 输入输出格式 输入格式: 一个正整数 n 输出格式: 使得 x^x 达到 n 位数字的最小正整数 x ...
- iOS 根据文字字数动态确定Label宽高
iOS7中用以下方法 - (CGSize)sizeWithAttributes:(NSDictionary *)attrs; 替代过时的iOS6中的- (CGSize)sizeWithFont:(UI ...
- require--按需加载js与模块化
1.html中: “<script type="text/javascript" src="js/require.js" data-main=" ...
- Unity 打包PC和安卓的路径注意事项
if UNITY_STANDALONE_WIN || UNITY_EDITOR return Application.persistentDataPath + "/LocalData&quo ...