[Linked List]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* p = head,*next=NULL,*pre=NULL;
- while(p && p->val==val){
- next = p->next;
- delete(p);
- p = next;
- head = p;
- }
- while(p){
- if(p->val == val){
- pre->next = p->next;
- delete(p);
- p = pre->next;
- }else{
- pre=p;
- p=p->next;
- }
- }
- return head;
- }
- };
[Linked List]Remove Linked List Elements的更多相关文章
- 【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 ...
- [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 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 ...
随机推荐
- javascript模式——Decorator
Decorator 模式是一种结构型模式,他意在促进代码的复用,是塑造子类的一个方式. 这种想法是基于,新增的属性,对于对象来说不是必须的基本功能.我们为特殊的对象添加自己的方法,而不是重新创建一个类 ...
- jquery创建动态的div
参考:http://blog.csdn.net/ge_zhiqiang/article/details/6958230
- JQ选择器
jQuery 的选择器可谓之强大无比,这里简单地总结一下常用的元素查找方法 $("#myELement") 选择id值等于myElement的元素,id值不能重复在文档中只能有一个 ...
- URL & webkitURL
base64带来的坑 在web中想要是实现图片在线预览的方式有几种 1.先上传 2.使用FileReader对象 3.URL.createObjectURL(file|blob) base64 能将图 ...
- B - A + B Again
Description There must be many A + B problems in our HDOJ , now a new one is coming. Give yo ...
- sqlite在c++中的使用方法
1.需要下载的文件 http://pan.baidu.com/s/1c06NpzM 2.执行文件shell的编译 3.在c++中如何使用 #include <stdio.h> # ...
- Android 创建自定义布局
我们所有的控件都是继承至View类的,而所有的布局都是继承至ViewGroup的,所以我们也可以继承某个view类来实现我们自己的布局或者控件. 引入布局 我们新建一个title.xml的layout ...
- Toast添加动画
WindowManger wm =(WindowManger)context.getSystemService(Context.WINDOW_SERVICE); View view = Toast.m ...
- Android 展示键盘时候布局被修改的问题
解决方法,在mainfest.xml中,对那个Activity加: <activity android:name=".activity.HomeActivity"androi ...
- Android manifest
manifest是Android应用程序的配置文,在这里指定了改程序的资源.权限等内容,一般的manifest文件如下 <manifest xmlns:android="http:// ...