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


题目标签:Linked List

  题目给了我们一个 链表 和一个 val,让我们把链表中 等于 val 的点都去除。

  情况1: 如果head.val == val,那么设 head = head.next,同时cursor 也要重新设定。

  情况2: 如果cursor.next.val == val 那么把cursor 连到 下下个点,跳过中间点。

  情况3: 如果cursor.next.val != val 的话,那么移动cursor 去下一个点。

Java Solution:

Runtime beats 50.37%

完成日期:06/10/2017

关键词:singly-linked list

关键点:检查cursor.next.val

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

参考资料:N/A

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

LeetCode 203. Remove Linked List Elements (移除链表中的项)的更多相关文章

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

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

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

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

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

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

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

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

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

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

  7. (easy)LeetCode 203.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. fragment间的数据传递

    今天我将要讲的是fragment间的数据是如何进行传递的.这里我将举个简单的例子. 首先要有个MainActivity,它托管了MainFragment,而MainFragment又托管了DatePi ...

  2. 09Microsoft SQL Server 表数据插入,更新,删除

    Microsoft SQL Server 表数据插入,更新,删除 向表中插入数据 INSERT INTO insert into tb1 values(0004,'张凤凤') insert into ...

  3. 反片语(Ananagrams,Uva 156)

    输入一些单词,找出所有满足如下条件的单词:该单词不能通过字母重排,得到输入文 本中的另外一个单词.在判断是否满足条件时,字母不分大小写,但在输出时应保留输入中 的大小写,按字典序进行排列(所有大写字母 ...

  4. [USACO] 奶牛混合起来 Mixed Up Cows

    题目描述 Each of Farmer John's N (4 <= N <= 16) cows has a unique serial number S_i (1 <= S_i & ...

  5. NFV

    转载: NFV介绍定义:NFV,即网络功能虚拟化,Network Function Virtualization.通过使用x86等通用性硬件以及虚拟化技术,来承载很多功能的软件处理.从而降低网络昂贵的 ...

  6. Win2008 Server下配置安装IIS

    最近又买了台服务器,接下来就是配置环境啦. 接下来接记录一下IIS的配置过程. 首先找到服务器管理器 打开后找到角色,点击添加角色 处理添加角色向导 勾选Web服务器(IIS) 点击添加必要功能 然后 ...

  7. list tuple dict (列表,元祖,字典间的相互转换)

    #-*- coding:UTF-8 -*- #author:RXS002 #1.字典 dict = {'name':'Zara','age':7,'class':'First'} #字典转换为字符串, ...

  8. Codeforces 934D/933B - A Determined Cleanup

    传送门:http://codeforces.com/contest/934/problem/D 给定两个正整数p(p≥1).k(k>1).多项式f(x)的系数的取值集合为{0,1,2,...,k ...

  9. Shiro_DelegatingFilterProxy

    1.DelegatingFilterProxy实际上是Filter的一个代理对象.默认情况下,Spring会到IOC容器中查找与<filter-name>对应的filter bean.也可 ...

  10. zoj 3768 Continuous Login

    Pierre is recently obsessed with an online game. To encourage users to log in, this game will give u ...