LeetCode 203. Remove Linked List Elements (移除链表中的项)
2024-08-31 00:13:03
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/
最新文章
- 阅读笔记 1 火球 UML大战需求分析
- LightOj 1215 - Finding LCM(求LCM(x, y)=L中的 y )
- 总结css之内联图片的优缺点
- c++ exports def文件
- processor, memory, I/O
- 视频FMS服务器带宽成本分析
- Android:AysncTask异步加载
- skip32
- lightoj1027(期望dp)
- python学习笔记之三:字典,当索引不好用时
- GridView动态增加行
- Codeforces Round #102 (Div. 2) 题解
- MyEclipse2014安装图解
- iOS中类单例方法的一种实现
- 【MySQL】5.7 复制
- [ 10.08 ]CF每日一题系列—— 602B
- UDP template 代码
- Modbus库开发笔记之一:实现功能的基本设计(转)
- python基础学习之路No.2 数据类型
- Ext3.4--TreeGridDemo
热门文章
- HDU_1789_doing homework again_贪心
- HDU_1180_诡异的楼梯_BFS
- HDU_1561_The more, The Better_树型dp
- c# 常用 Common
- tf idf公式及sklearn中TfidfVectorizer
- Bat 脚本(常用命令)
- <;MyBatis>;入门二 全局配置文件
- 51nod 1285 山峰和分段
- POJ -棋盘问题
- poj 2114 树的分治 可作模板