题目:

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. Cocos2D iOS之旅:如何写一个敲地鼠游戏(五):设置背景

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...

  2. OpenCV计算物体的重心坐标(2值图像)

    效果图: 代码: // FindGravity.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream& ...

  3. Java-IO之FilterInputStream和FilterOuptStream

    FilterInputStream的作用是用来封装其他的输入流,并为它们提供了额外的功能,它的常用的子类有BufferedInputStream和DataInputStream.FilterOutpu ...

  4. android scrollview嵌套listview计算高度的问题

    ScrollView中只能放一个控件,一般都放LinearLayout,orientation属性值为vertical.在LinearLayout中放需要呈现的内容.ListView也在其中,List ...

  5. Html标签中thead、tbody、tfoot的作用

    Html标签中thead.tbody.tfoot的作用 为了让大表格(table)在下载的时候可以分段的显示,就是说在浏览器解析HTML时,table是作为一个整体解释的,使用TBODY可以优化显示. ...

  6. Gradle 1.12 翻译——第十八章. 日志

    有关其他已翻译的章节请关注Github上的项目:https://github.com/msdx/gradledoc/tree/1.12,或访问:http://gradledoc.qiniudn.com ...

  7. UNIX环境高级编程——时间和日期

    由UNIX内核提供的基本时间服务是计算自国际标准时间公元1970年1月1日00:00:00以来经过的秒数.这种秒数是以数据类型time_t表示. 1.     time函数返回当前时间和日期: tim ...

  8. Cracking the Coding Interview:: 寻找有环链表的环路起始节点

    给定一个有环链表,实现一个算法返回环路的开头节点. 这个问题是由经典面试题-检测链表是否存在环路演变而来.这个问题也是编程之美的判断两个链表是否相交的扩展问题. 首先回顾一下编程之美的问题. 由于如果 ...

  9. 总账balance表

    SELECT gb.period_net_dr, --期间发生额        gb.period_net_cr, --期间发生额        gb.project_to_date_dr, --账户 ...

  10. 【leetcode77】Single Number

    一题目描述: 给定一个数组,只有一个数字出现一次,其余都是两次,判断那个数字 思路: 不断取出数据进行异或,最后一个数字,因为相同的数字会抵消 代码: public class Solution { ...