LeetCode 203. Remove Linked List Elements (移除链表中的项)
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 (移除链表中的项)的更多相关文章
- [LeetCode] 203. Remove Linked List Elements 移除链表元素
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- LeetCode 203. Remove Linked List Elements 移除链表元素 C++/Java
Remove all elements from a linked list of integers that have value val. Example: Input: ->->-& ...
- 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题要求 ...
- [LintCode] Remove Linked List Elements 移除链表元素
Remove all elements from a linked list of integers that have value val. Have you met this question i ...
- [LeetCode] Remove Linked List Elements 移除链表元素
Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...
- 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 -- ...
- (easy)LeetCode 203.Remove Linked List Elements
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- Java [Leetcode 203]Remove Linked List Elements
题目描述: Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> ...
- [LeetCode] 203. Remove Linked List Elements 解题思路
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
随机推荐
- phpcms v9文章内容页调用上一篇下一篇的方法(转)
phpcms v9文章内容页调用上一篇下一篇的方法如下,魔客吧(www.moke8.com)提示您直接摘取如下代码中的红色部分即可: 上一篇:{$previous_page[url]}" t ...
- CAD使用DeleteXData删除数据(网页版)
主要用到函数说明: MxDrawEntity::DeleteXData 删除扩展数据,详细说明如下: 参数 说明 pzsAppName 删除的扩展数据名称,如果为空,删除所有扩展数据 js代码实现如下 ...
- Android studio升级后原有项目无法正常编译运行问题
Android studio工具升级后Gradle版本问题 背景 升级AndroidStudio到最新版本后,原来可正常编译输出AndroidTest的项目无法正常编译通过. 原因 升级后的Andro ...
- 如何在MONO 3D寻找最短路路径
前段时间有个客户说他们想在我们的3D的机房中找从A点到B点的最短路径,然而在2D中确实有很多成熟的寻路算法,其中A*是最为常见的,而这个Demo也是用的A*算法,以下计算的是从左上角到右下角的最短路径 ...
- TWaver MONO模板库新鲜出炉 精彩纷呈
MONO Design在线3D建模平台网站, www.mono-design.cn,开发组的成员们已经开始紧锣密鼓的对这个平台进行内测.在之前的文章里,我们提到用户可以获得多种多样的TWaver官方模 ...
- Spring 4 整合RMI技术及发布多个服务(xjl456852原创)
rmi需要建立两个项目,一个是服务端的项目,一个是客户端的项目.服务端项目启动后,再启动客户端项目去调用服务端的方法. 我们建立两个maven项目: pom.xml配置: <?xml versi ...
- Spring AOP学习(六)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- MSSQL获取指定日期的SQL语句
--1.一个月第一天的 SELECT DATEADD(mm,DATEDIFF(mm,0,getdate()),0) --2.本周的星期一 SELECT DATEADD(wk,DATEDIFF(wk,0 ...
- HDU 1542 Atlantics 线段树+离散化扫描
将 x 轴上的点进行离散化,扫描线沿着 y 轴向上扫描 每次添加一条边不断找到当前状态有效边的长度 , 根据这个长度和下一条边形成的高度差得到一块合法的矩形的面积 #include<iostre ...
- jQuery动态添加表格1
用jquery的append方法在指定行的后面新增一行tr,会把新增的行的html追加到指定行的html里面 content +="<tr><td>123</t ...