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. 阅读笔记 1 火球 UML大战需求分析
  2. LightOj 1215 - Finding LCM(求LCM(x, y)=L中的 y )
  3. 总结css之内联图片的优缺点
  4. c++ exports def文件
  5. processor, memory, I/O
  6. 视频FMS服务器带宽成本分析
  7. Android:AysncTask异步加载
  8. skip32
  9. lightoj1027(期望dp)
  10. python学习笔记之三:字典,当索引不好用时
  11. GridView动态增加行
  12. Codeforces Round #102 (Div. 2) 题解
  13. MyEclipse2014安装图解
  14. iOS中类单例方法的一种实现
  15. 【MySQL】5.7 复制
  16. [ 10.08 ]CF每日一题系列—— 602B
  17. UDP template 代码
  18. Modbus库开发笔记之一:实现功能的基本设计(转)
  19. python基础学习之路No.2 数据类型
  20. Ext3.4--TreeGridDemo

热门文章

  1. HDU_1789_doing homework again_贪心
  2. HDU_1180_诡异的楼梯_BFS
  3. HDU_1561_The more, The Better_树型dp
  4. c# 常用 Common
  5. tf idf公式及sklearn中TfidfVectorizer
  6. Bat 脚本(常用命令)
  7. <MyBatis>入门二 全局配置文件
  8. 51nod 1285 山峰和分段
  9. POJ -棋盘问题
  10. poj 2114 树的分治 可作模板