[抄题]:

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

[暴力解法]:

时间分析:

空间分析:

[思维问题]:

  1. 链表结构可能会改变,忘记用dummy node了,做链表题时提前牢记
  2. 两个链表采用2个指针,一个链表用1个就够了

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. dummy初始化时,head = dummy是为了把head指针的指向从数字转移到dummy,使其名正言顺。
  2. 理解dummy一直是空节点,最后返回的还是dummy.next

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

  1. 两个链表采用2个指针,一个链表用1个就够了

[复杂度]:Time complexity: O(n) Space complexity: O(1)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[关键模板化代码]:

ListNode dummy = new ListNode(0);
dummy.next = head;
head = dummy; //remove
while (head.next != null) {
if (head.next.val == val) {
head.next = head.next.next;
}else {
head = head.next;
}
} return dummy.next;

dummy node

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

/**
* Definition for ListNode
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/ public class Solution {
/**
* @param head: a ListNode
* @param val: An integer
* @return: a ListNode
*/
public ListNode removeElements(ListNode head, int val) {
//dummy
ListNode dummy = new ListNode(0);
dummy.next = head;
head = dummy; //remove
while (head.next != null) {
if (head.next.val == val) {
head.next = head.next.next;
}else {
head = head.next;
}
} return dummy.next;
}
}

删除链表中的元素 · Remove Linked List Elements的更多相关文章

  1. LintCode之删除链表中的元素

    题目描述 我的代码 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode n ...

  2. 203 Remove Linked List Elements 删除链表中的元素

    删除链表中等于给定值 val 的所有元素.示例给定: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6返回: 1 --& ...

  3. [Swift]LeetCode203. 移除链表元素 | Remove Linked List Elements

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

  4. LintCode #452 删除链表中的元素

    方法很笨拙,被链表给绕住了,抽空在整理一下. /** * Definition for ListNode * public class ListNode { * int val; * ListNode ...

  5. lintcode:删除链表中指定元素

    题目 删除链表中等于给定值val的所有节点. 样例 给出链表 1->2->3->3->4->5->3, 和 val = 3, 你需要返回删除3之后的链表:1-> ...

  6. LeetCode Delete Node in a Linked List (删除链表中的元素)

    题意:给一个将要删除的位置的指针,要删除掉该元素.被删元素不会是链尾(不可能删得掉). 思路:将要找到前面的指针是不可能了,但是可以将后面的元素往前移1位,再删除最后一个元素. /** * Defin ...

  7. 【LintCode】删除链表中的元素

    问题分析: 声明当前指针和上一个指针即可. 问题求解: public class Solution { public ListNode removeElements(ListNode head, in ...

  8. [LintCode]删除链表中的元素

    问题分析: 声明当前指针和上一个指针即可. 问题求解: public class Solution { public ListNode removeElements(ListNode head, in ...

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

随机推荐

  1. 实现C++标准库string类的简单版本

    代码如下: #ifndef STRING_H #define STRING_H #include <cassert> #include <utility> #include & ...

  2. promise的基础知识

    promise 相当于异步操作结果的占位符 它不会去订阅一个事件,也不会传递一个回调函数给目标函数,而是让函数返回一个promise,例如: let promise = readFile('a.txt ...

  3. mysql 多条语句合并查询

    select count(*) from matches where StartTime > 1519315200 and endtime < 1519401600 and matchty ...

  4. spring注解-@Autowired。@Resource。@Service

    Spring的@Autowired注解.@Resource注解和@Service注解 什么是注解 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点: ...

  5. goaccess nginx日志分析工具简单使用

    goaccess 是一个比较方便的支持实时的日志分析工具,比较方便,同时安装&&配置简单 安装 centos yum yum install -y goaccess 运行 我的ngin ...

  6. OLEDB操作Excel

    使用OLEDB操作Excel 的方法 OleDbConnection conn = null;            try            {              //fileName ...

  7. sorting--codility

    lesson 6: sorting 1. Distinct 2. Triangle 2. MaxProductOfThree 4. NumberOfDiscIntersections lesson 6 ...

  8. 爬取爱奇艺电视剧url

    ----因为需要顺序,所有就用串行了---- import requests from requests.exceptions import RequestException import re im ...

  9. [转][ASP.NET]ASP.NET 预编译网站

    [转自]https://msdn.microsoft.com/zh-cn/library/ms227430(v=vs.80).aspx C:\Windows\Microsoft.NET\Framewo ...

  10. [ML] Gradient Descend Algorithm [Octave code]

    function [theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters) m = length(y); % n ...