56-Remove Linked List Elements
- Remove Linked List Elements My Submissions QuestionEditorial Solution
Total Accepted: 61924 Total Submissions: 215788 Difficulty: 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
思路:先找到第一个不等于指定值的首节点,作为返回值
另外在过程中过滤掉等于val值的节点并使前面一个不为val值得节点指向下一个不为val值的节点
时间复杂度:O(n)
空间复杂度:O(1)
/**
* 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 NULL;
while(head!=NULL&&head->val==val)head=head->next;//找到第一个头部
ListNode *p=head,*prep=head;
while(p!=NULL){
prep=p;
p=p->next;
while(p!=NULL&&p->val==val)p=p->next;//直到找到下一个不为val的节点并指向它
prep->next = p;
}
return head;
}
};
56-Remove Linked List Elements的更多相关文章
- [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
#203. Remove Linked List Elements Remove all elements from a linked list of integers that have val ...
- 【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 ...
- 【刷题-LeetCode】203. Remove Linked List Elements
Remove Linked List Elements Remove all elements from a linked list of integers that have value *val* ...
随机推荐
- [技术博客] K-Means算法
遇到的问题 在对微软\(OCR\)的\(api\)进行测试的过程中,我发现有时候它并不能分析出一个表格的形态,也就是说不知道每个文本对应在表格中的第几行第几列.但是它可以较为准确的给出这些文本的坐标. ...
- the Agiles Scrum Meeting 5
会议时间:2020.4.13 20:00 1.每个人的工作 今天已完成的工作 增量组:完成了增量开发的基础工作,初步完成了自动评测机制 issues:增量组:准备评测机制,增加仓库自动创建和管理 完善 ...
- 21.6.23 test
省选 模拟赛 今天考的是一套题目背景和描述会被[数据删除]的模拟赛. 犯了几个傻逼错. \(T1\) 把两种情况的概率看反了,写的暴力.\(35->5\) pts. \(T2\) 以为想到了正解 ...
- 2021CCPC华为云挑战赛 部分题题解
CDN流量调度问题 题看了没多久就看出来是\(DP\)的题,然后就设了状态\(f[i][j]\)表示到前\(i\)个点时已经用了\(j\)个节点的最小总代价,结果发现转移时\(O(nm^2)\),但这 ...
- poj 3537 Crosses and Crosses (SG)
题意: 1 × n 个格子,每人每次选一个格子打上叉(不得重复),如果一个人画完叉后出现了连续的三个叉,则此人胜. 给n,判断先手胜还是先手败. 思路: 假设选择画叉的位置是i,则对方只能在前[1,i ...
- 设计模式(1-3)-动态代理(WeakCache的运用)
阅读本篇文章前,请事先阅读 理解Java的强引用.软引用.弱引用和虚引用. 看看什么是强引用.什么是弱引用及它们的用途,很必要!!! 上一节讲到,获取对应的代理类时,首先会从缓存中去拿,若拿不到才会去 ...
- Android Jetpack Compose 引入示例工程
引入 Jetpack Compose 示例工程 去GitHub上找到Compose的示例工程 https://github.com/android/compose-samples ,clone到本地 ...
- Docker配置tomcat端口映射后无法访问(404)
1.配置tomcat端口映射 2.访问测试 3.修改webapps文件,webapps.dist是所需文件 4.虚拟机中重新访问 5.在主机也能访问成功(注意路径为虚拟机IP,不是localhost)
- Connected to an idle instance.
Connected to an idle instance:连接到空闲实例 原因:数据库或者监听没启动
- k8s入坑之路(7)kubernetes设计精髓List/Watch机制和Informer模块详解
1.list-watch是什么 List-watch 是 K8S 统一的异步消息处理机制,保证了消息的实时性,可靠性,顺序性,性能等等,为声明式风格的API 奠定了良好的基础,它是优雅的通信方式,是 ...