一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

我的个人博客已创建,欢迎大家持续关注!

一天一道leetcode系列依旧在csdn上继续更新,除此系列以外的文章均迁移至我的个人博客

另外,本系列文章已整理并上传至gitbook,网址:点我进

欢迎转载,转载请注明出处!

(一)题目

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* pre = NULL;
        ListNode* p = head;
        while(p){
            if(p->val==val){//与给定值相等
                while(p&&p->val==val) p=p->next;//连续相等的情况
                if(pre==NULL) head = p;//如果需要删除头节点,则需要更新头节点
                else pre->next = p;
            }
            else{
                pre = p;
                p=p->next;
            }
        }
        return head;
    }
};

【一天一道Leetcode】#203.Remove Linked List Elements的更多相关文章

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

  2. LeetCode 203. Remove Linked List Elements (移除链表中的项)

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

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

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

  4. Java for LeetCode 203 Remove Linked List Elements

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

  5. (easy)LeetCode 203.Remove Linked List Elements

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

  6. Java [Leetcode 203]Remove Linked List Elements

    题目描述: Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> ...

  7. [LeetCode] 203. Remove Linked List Elements 解题思路

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

  8. LeetCode 203. Remove Linked List Elements 移除链表元素 C++/Java

    Remove all elements from a linked list of integers that have value val. Example: Input: ->->-& ...

  9. Leetcode 203 Remove Linked List Elements 链表

    去掉链表中相应的元素值 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next ...

  10. [leetcode]203. Remove Linked List Elements链表中删除节点

    这道题很基础也很重要 重点就是设置超前节点 public ListNode removeElements(ListNode head, int val) { //超前节点 ListNode pre = ...

随机推荐

  1. ●BZOJ 4822 [Cqoi2017]老C的任务

    题链: https://www.luogu.org/problemnew/show/P3755 (洛谷上数据范围给全了的) 题解: 树状数组,离线询问 (本来想弄一个二维树状数组/二维RMQ,然后直接 ...

  2. hihocoder 1035 : 自驾旅行 III

    描述 给定一棵含有 n 个结点的树,结点从 1 标号.你从 1 号结点驾车出发,希望遍历一些关键结点(访问到就好,不需要按照这些关键结点的输入顺序).每条边有两个权值,c0, c1 分别表示步行和驾车 ...

  3. 笔记14 Hello Spring MVC

    搭建一个SpringMVC所需要的包: 1.创建MySpringMVC项目,使用dynamic web project的方式. 2.导入jar包,放到/WebContent(WebRoot)/WEB- ...

  4. sublime text3中设置Emmet输入标签自动闭合

    项目后端前一段时间从C#转成了JAVA,在开发的过程中,由于HTML对标签的语法很宽松,比如这样:<img src="" alt="">在标签的结尾 ...

  5. rhel7 启动网络

    我装的是rhel7 服务器版本(在virtualbox虚拟机里),安装后默认不启动网络,另外还有很多命令也不能用,比如ifconfig, yum-config-manager等. 先解决网络问题: 切 ...

  6. ubuntu下安装 python 常用软件

    1.用于科学计算的常用包: sudo apt-get install python-numpy python-scipy python-matplotlib ipython ipython-noteb ...

  7. 克拉默法则(Cramer's Rule)的证明

    克拉默法则: 先说一下为什么要写这个,作为一个大一新生,必须要学的就包括了线性代数,而且线性代数等数学知识对计算机专业也有很大帮助.但是在学习过程中遇到一个讲解的不清楚的知识点(Cramer's Ru ...

  8. python笔记十三(高阶函数、装饰器)

    一.高阶函数 函数只要有以下两个特征中一个就可以称为高阶函数: a:函数名作为一个实参传入另一个函数中 b:函数的返回值中包含函数名 下面我们用代码来感受一下这两种形式: import time # ...

  9. PHP 安全 E-mail

    PHP E-mail 注入 首先,请看上一章中的 PHP 代码: <html> <body> <?php if (isset($_REQUEST['email'])) / ...

  10. 安卓onTextChanged参数解释及实现EditText字数监听 Editable使用

    原作者部分修改部分 补充部分 补充部分2 补充部分3 补充部分4 Editable 尊重原作者:此篇文章是借鉴原作者地址 的博文 并进行修改和增加补充说明,我只是补充和修改: 我感觉这篇文章经过我的补 ...