Total Accepted: 43183 Total Submissions: 160460 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

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode(int x) : val(x), next(NULL) {}
  7. * };
  8. */
  9. class Solution {
  10. public:
  11. ListNode* removeElements(ListNode* head, int val) {
  12. ListNode* p = head,*next=NULL,*pre=NULL;
  13. while(p && p->val==val){
  14. next = p->next;
  15. delete(p);
  16. p = next;
  17. head = p;
  18. }
  19. while(p){
  20. if(p->val == val){
  21. pre->next = p->next;
  22. delete(p);
  23. p = pre->next;
  24. }else{
  25. pre=p;
  26. p=p->next;
  27. }
  28. }
  29. return head;
  30. }
  31. };

[Linked List]Remove Linked List Elements的更多相关文章

  1. 【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 ...

  2. [LintCode] Remove Linked List Elements 移除链表元素

    Remove all elements from a linked list of integers that have value val. Have you met this question i ...

  3. Leetcode-203 Remove Linked List Elements

    #203.   Remove Linked List Elements Remove all elements from a linked list of integers that have val ...

  4. 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题要求 ...

  5. 【LeetCode】203. Remove Linked List Elements

    Remove Linked List Elements Remove all elements from a linked list of integers that have value val. ...

  6. 203. Remove Linked List Elements【easy】

    203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...

  7. LeetCode Remove Linked List Elements 删除链表元素

    题意:移除链表中元素值为val的全部元素. 思路:算法复杂度肯定是O(n),那么就在追求更少代码和更少额外操作.我做不出来. /** * Definition for singly-linked li ...

  8. LeetCode_203. Remove Linked List Elements

    203. Remove Linked List Elements Easy Remove all elements from a linked list of integers that have v ...

  9. LeetCode--LinkedList--203. Remove Linked List Elements(Easy)

    203. Remove Linked List Elements(Easy) 题目地址https://leetcode.com/problems/remove-linked-list-elements ...

随机推荐

  1. javascript模式——Decorator

    Decorator 模式是一种结构型模式,他意在促进代码的复用,是塑造子类的一个方式. 这种想法是基于,新增的属性,对于对象来说不是必须的基本功能.我们为特殊的对象添加自己的方法,而不是重新创建一个类 ...

  2. jquery创建动态的div

    参考:http://blog.csdn.net/ge_zhiqiang/article/details/6958230

  3. JQ选择器

    jQuery 的选择器可谓之强大无比,这里简单地总结一下常用的元素查找方法 $("#myELement") 选择id值等于myElement的元素,id值不能重复在文档中只能有一个 ...

  4. URL & webkitURL

    base64带来的坑 在web中想要是实现图片在线预览的方式有几种 1.先上传 2.使用FileReader对象 3.URL.createObjectURL(file|blob) base64 能将图 ...

  5. B - A + B Again

    Description There must be many A + B problems in our HDOJ , now a new one is coming.         Give yo ...

  6. sqlite在c++中的使用方法

    1.需要下载的文件      http://pan.baidu.com/s/1c06NpzM 2.执行文件shell的编译 3.在c++中如何使用 #include <stdio.h> # ...

  7. Android 创建自定义布局

    我们所有的控件都是继承至View类的,而所有的布局都是继承至ViewGroup的,所以我们也可以继承某个view类来实现我们自己的布局或者控件. 引入布局 我们新建一个title.xml的layout ...

  8. Toast添加动画

    WindowManger wm =(WindowManger)context.getSystemService(Context.WINDOW_SERVICE); View view = Toast.m ...

  9. Android 展示键盘时候布局被修改的问题

    解决方法,在mainfest.xml中,对那个Activity加: <activity android:name=".activity.HomeActivity"androi ...

  10. Android manifest

    manifest是Android应用程序的配置文,在这里指定了改程序的资源.权限等内容,一般的manifest文件如下 <manifest xmlns:android="http:// ...