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.

每次遇到链表操作,树操作的时候我的大脑就会messed up. 各种考虑不周全,各种edge case 失败。

思路分两步,第一步删除头,即比如要删除6,而链表是6->6->3->2。先把以目标6开头的链表变成不已目标开头的链表。

第二步就便利链表,如果发现next 是目标,就把当前的next 变为next->next。

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* removeElements(struct ListNode* head, int val) {
struct ListNode* p = NULL;
while (head != NULL && head->val == val) {
head = head->next;
}
if (head == NULL) return NULL; p = head;
while (p != NULL) {
if (p->next != NULL && p->next->val == val) {
p->next = p->next->next;
} else {
p = p->next;
}
}
return head;
}

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

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

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

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

    Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...

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

  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. 【刷题-LeetCode】203. Remove Linked List Elements

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

  7. 203. Remove Linked List Elements - LeetCode

    Question 203. Remove Linked List Elements Solution 题目大意:从链表中删除给定的数 思路:遍历链表,如果该节点的值等于给的数就删除该节点,注意首节点 ...

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

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

  9. LeetCode_203. Remove Linked List Elements

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

随机推荐

  1. jquery 实现类似于弹幕效果

    在别人网站中看到一个类似于弹幕的效果,闲来无事用jquery写了个备用~~ <!DOCTYPE html> <meta charset="utf-8"> & ...

  2. JBPM4.4业务流程管理框架详细解读

    1. 什么是JBPM4.4业务流程管理框架? JBPM,全称是JavaBusiness Process Management(业务流程管理),它是覆盖了业务流程管理.工作流.服务协作等领域的一个开源的 ...

  3. Ubuntu 14.10安装手记

    1. Ubuntu系统安装 按照 www.linuxidc.com/Linux/2014-04/100369.htm 的安装方法即可. 配置文件有两个需要注意的地方.一.搞清楚电脑的分区表,c盘是hd ...

  4. Internet Download Manager 6.27.1 中文特别版(IDM)

    软件介绍: 软件名称:Internet Download Manager(IDM) 软件大小:5.09M软件语言:简体中文 软件官网:http://www.internetdownloadmanage ...

  5. oracle 修改密码

    [oracle@t-ecif03-v-szzb ~]$ sqlplus / as sysdba SQL*Plus: Release 11.2.0.3.0 Production on Mon Nov 1 ...

  6. JavaScript学习1

    http://blog.csdn.net/lilongsheng1125/article/details/8479391 数据类型 1.基础数据类型 数值型.字符串型.逻辑型.undefined.nu ...

  7. [转载]用 grub2 启动 clover.iso 来启动 OS X

    这个帖子只用来解决特定问题,是楼主这两天辛苦的结晶,如果你遇到了跟我差不多的情形,你就可以尝试这个解决方案. 特定情景:1.不管你的机器支不支持 UEFI ,反正你现在是用传统 BISO + MBR ...

  8. Java Web编程技术学习要点及方向

    学习编程技术要点及方向亮点: 传统学习编程技术落后,应跟著潮流,要对业务聚焦处理.要Jar, 不要War:以小为主,以简为宝,集堆而成.去繁取简 Spring Boot,明日之春(future of ...

  9. 面试题目——《CC150》线程与锁

    package cc150.thread_lock; public class RunnableThreadExample implements Runnable{ public int count ...

  10. 收藏一些好用的fifo

    1.Nordic库中的 E:\nRF52_SDK_0.9.2_dbc28c9\components\libraries\fifo app_fifo.c /* Copyright (c) 2013 No ...