203. Remove Linked List Elements【easy】
203. Remove Linked List Elements【easy】
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
Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.
解法一:
- /**
- * 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) {
- if (head == NULL) {
- return head;
- }
- ListNode * dummy = new ListNode(INT_MIN);
- dummy->next = head;
- head = dummy;
- while (head != NULL && head->next != NULL) {
- if (head->next->val == val) {
- while (head->next != NULL && head->next->val == val) {
- ListNode * temp = head->next;
- free(temp);
- head->next = head->next->next;
- }
- }
- else {
- head = head->next;
- }
- }
- return dummy->next;
- }
- };
里面的while可以不用,因为这个题和(82. Remove Duplicates from Sorted List II)不一样,那个题你根本不知道val是什么,所以只用一个if是不行的。但是这个题你知道val是什么,所以可以省略里面的while。
解法二:
- /**
- * 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) {
- if (head == NULL) {
- return head;
- }
- ListNode * dummy = new ListNode(INT_MIN);
- dummy->next = head;
- head = dummy;
- while (head != NULL && head->next != NULL) {
- if (head->next->val == val) {
- ListNode * temp = head->next;
- free(temp);
- head->next = head->next->next;
- }
- else {
- head = head->next;
- }
- }
- return dummy->next;
- }
- };
精简写法
解法三:
- public ListNode removeElements(ListNode head, int val) {
- if (head == null) return null;
- head.next = removeElements(head.next, val);
- return head.val == val ? head.next : head;
- }
递归,参考了@renzid 的代码
203. Remove Linked List Elements【easy】的更多相关文章
- 203. Remove Linked List Elements【Easy】【未排序链表删除其中的给定值】
Remove all elements from a linked list of integers that have value val. Example: Input: 1->2-> ...
- 452. Remove Linked List Elements【Naive】
Remove all elements from a linked list of integers that have value val. Example Given 1->2->3- ...
- 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题要求 ...
- 141. Linked List Cycle【easy】
141. Linked List Cycle[easy] Given a linked list, determine if it has a cycle in it. Follow up:Can y ...
- 203. Remove Linked List Elements - LeetCode
Question 203. Remove Linked List Elements Solution 题目大意:从链表中删除给定的数 思路:遍历链表,如果该节点的值等于给的数就删除该节点,注意首节点 ...
- 【LeetCode】203. Remove Linked List Elements
Remove Linked List Elements Remove all elements from a linked list of integers that have value val. ...
- 【刷题-LeetCode】203. Remove Linked List Elements
Remove Linked List Elements Remove all elements from a linked list of integers that have value *val* ...
- 【leetcode】Remove Linked List Elements(easy)
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- 【一天一道Leetcode】#203.Remove Linked List Elements
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 我的个人博客已创建,欢迎大家持续关注! 一天一道le ...
随机推荐
- newcoder contest 114 B - 求值2
记得这是一个组合数卷积的板子题. ∑ C(A,i) * C(B,D-i) = C(A+B,D) 然后就直接做了. #include<cstdio> #include<cctyp ...
- 【找规律】Codeforces Round #392 (Div. 2) C. Unfair Poll
C. Unfair Poll time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- 【分块】bzoj2120 数颜色
分块,在每个点记录一下它之前离它最近的相同颜色的位置pre[i],显然问题转化成了求[l,r]中pre[i]<l的值的个数. 这是分块擅长的,在每个块内记录有序表,查询时对零散的暴力,整块的二分 ...
- 安装notepad++ in ubuntu16.04
一.安装notepad++ Ubuntu下的安装方法: sudo add-apt-repository ppa:notepadqq-team/notepadqq sudo apt-get update ...
- javascript快速入门9--引用类型
引用类型通常叫做类(class),也就是说,遇到引用值,所处理的就是对象. 注意:从传统意义上来说,ECMAScript 并不真正具有类.事实上,除了说明不存在类,在 ECMA-262 中根本没有出现 ...
- Spark Streaming on Kafka解析和安装实战
本课分2部分讲解: 第一部分,讲解Kafka的概念.架构和用例场景: 第二部分,讲解Kafka的安装和实战. 由于时间关系,今天的课程只讲到如何用官网的例子验证Kafka的安装是否成功.后续课程会接着 ...
- 【Hadoop】HDFS源码解读
1.open流程 2.get DFS流程: 3.获取block信息流程
- kafka启动报错:kafka.common.KafkaException: Failed to acquire lock on file .lock
kafka 异常退出后重启时遇到的问题 解决: 执行 netstat -lnp|grep 9092 在执行结果中找到进程号执行 kill -9 进程号再尝试启动Kafka
- BIEE-CSS样式大全
字体属性:(font) 大小 {font-size: x-large;}(特大) xx-small;(极小) 一般中文用不到,只要用数值就可以,单位:PX.PD 样式 {font-style: obl ...
- 移动负载均衡技术(MBL)
移动负载均衡技术(MBL) 转至元数据结尾 附件:5 被admin添加,被admin最后更新于四月 27, 2015 转至元数据起始 互联网技术发展到今天,已经进入移动时代,很多在传统CS和BS的 ...