题目:

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

思路:

  • 题意:有序列表里面去掉给定的元素
  • 遍历发现相同的,就去掉,prev.next != null,发现了prev.next = prev.next.next
  • 考虑头部满足条件的情况,那就一直删除下去
  • -

代码:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode removeElements(ListNode head, int val) {
        if(head == null){
            return null;
        }
        while(head.val == val){
            head = head.next;
            if(head == null){
                return null;
            }
        }
        ListNode prev = head;
        while(prev.next != null){
            while(prev.next.val == val){
                prev.next = prev.next.next;
                if(prev.next == null){
                return head;
            }
            }
            prev = prev.next;
        }
        return head;
    }
}

LeetCode(52)-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. 【leetcode】Remove Linked List Elements(easy)

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

  5. 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 -- ...

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

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

  7. leetcode:Remove Linked List Elements

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

  8. Java [Leetcode 203]Remove Linked List Elements

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

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

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

随机推荐

  1. 2.2、Android Studio通过注解提升代码检测

    使用像Lint这样的代码检测工具可以帮助你发现问题和提升代码,但是代码检测在有些地方很难应用.例如,Android的资源ID,使用一个int类型来表示字符.图像.颜色或者其他资源类型所以代码检测工具不 ...

  2. 随机采样和随机模拟:吉布斯采样Gibbs Sampling

    http://blog.csdn.net/pipisorry/article/details/51373090 吉布斯采样算法详解 为什么要用吉布斯采样 通俗解释一下什么是sampling. samp ...

  3. android git上开源的项目收藏

    本文为那些不错的Android开源项目第一篇--个性化控件(View)篇,主要介绍Android上那些不错个性化的View,包括ListView.ActionBar.Menu.ViewPager.Ga ...

  4. android最新更新方法

    使用SDK Manager更新时出现问题Failed to fetch URL https://dl-ssl.google.com/android/repository/repository-6.xm ...

  5. Linux下多线程编程遇到的一些问题

    今天在学习了Linux的多线程编程的基础的知识点.于是就试着做了一个简单的Demo.本以为会得到预期的结果.不成想却遇到了意想不到的问题. 代码展示 我的C 代码很简单,就是一个简单的示例程序,如下: ...

  6. UNIX网络编程——僵尸进程

         在fork()/exec()过程中,假设子进程结束时父进程仍存在,而父进程fork()之前既没安装SIGCHLD信号处理函数调用waitpid()等待子进程结束,又没有显式忽略该信号,则子进 ...

  7. iOS开发xcode报错:"xxxxxx"has been modified since the precompiled header was built

    最近做iOS开发,老是遇到这种问题,正好我有两个版本的Xcode,换了另一个,就正常了,所以也一直没解决. 今天又遇到这个问题,就查了一下资料,找到了解决办法,在次记录一下方便查找,就是在Xcode中 ...

  8. msm8974 camera driver添加新摄像头kernel hal修改

    添加一款新摄像头流程 1添加sensor kernel driver, 主要实现上电.rst.pwd.mclk等power setting,sensor prob & sensor  i2c ...

  9. Linux下修改主机名步骤

    Linux下修改主机名为gpdb 步骤一.运行vi /etc/sysconfig/network命令 NETWORKING=yesHOSTNAME=gpdb 步骤二.运行hostname gpdb命令 ...

  10. Linux多线程实践(9) --简单线程池的设计与实现

    线程池的技术背景 在面向对象编程中,创建和销毁对象是很费时间的,因为创建一个对象要获取内存资源或者其它更多资源.在Java中更是如此,虚拟机将试图跟踪每一个对象,以便能够在对象销毁后进行垃圾回收.所以 ...