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

Solution1: 不使用头结点。

注意事项:while停止的条件要特备注意;各种输入情况都要考虑到;如果首节点就是要删除的节点肿么办,等等问题。

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode removeElements(ListNode head, int val) {
if(head == null)
return null;
if(head.next==null && head.val==val)
return null;
if(head.next==null && head.val!=val)
return head;
ListNode p = head; while(head.next!=null && head.val==val){
p = head;
head = head.next;
p.next = null;
} p = head;
ListNode q = p.next;
while(p.next!=null){
q = p.next;
if(q.val == val){
p.next = q.next;
q.next = null;
}
else{
p = q;
q = p.next;
}
}
if(head.val==val)//做最后的检查,看是否首节点是要删除的元素
return null;
return head; }
}

Solution2: 使用附加头结点。

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

LeetCode--Remove Linked List Element的更多相关文章

  1. LeetCode Remove Linked List Elements 删除链表元素

    题意:移除链表中元素值为val的全部元素. 思路:算法复杂度肯定是O(n),那么就在追求更少代码和更少额外操作.我做不出来. /** * Definition for singly-linked li ...

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

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

  3. [LeetCode] Remove Linked List Elements

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

  4. 建立链表的虚拟头结点 203 Remove Linked List Element,82,147,148,237

    该逻辑对于删除第一个元素不适用. 这样的代码不优美 /** * Definition for singly-linked list. * struct ListNode { * int val; * ...

  5. [LeetCode] Remove Element 移除元素

    Given an array and a value, remove all instances of that value in place and return the new length. T ...

  6. 【LeetCode】237 & 203 - Delete Node in a Linked List & Remove Linked List Elements

    237 - Delete Node in a Linked List Write a function to delete a node (except the tail) in a singly l ...

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

  8. 【LeetCode】203. Remove Linked List Elements

    Remove Linked List Elements Remove all elements from a linked list of integers that have value val. ...

  9. 【刷题-LeetCode】203. Remove Linked List Elements

    Remove Linked List Elements Remove all elements from a linked list of integers that have value *val* ...

  10. 203. Remove Linked List Elements - LeetCode

    Question 203. Remove Linked List Elements Solution 题目大意:从链表中删除给定的数 思路:遍历链表,如果该节点的值等于给的数就删除该节点,注意首节点 ...

随机推荐

  1. ExtJS动态切换主题

    ExtJS动态切换主题         在Sencha Cmd构建的Ext程序中怎么去动态切换主题,目前看好像只能单一切换,但是在官网文档找到了答案 Resource Management在上一节通过 ...

  2. 网站如何使用https

    阿里云提供了免费的证书, 先去申请免费的https证书 https://common-buy.aliyun.com/?spm=5176.10695662.958455.3.1f0c7d54HhNTG4 ...

  3. HTML5+ MUI实现ajax的一个demo

    index.html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> &l ...

  4. Hadoop(11)-MapReduce概述和简单实操

    1.MapReduce的定义 2.MapReduce的优缺点 优点 缺点 3.MapReduce的核心思想 4.MapReduce进程 5.常用数据序列化类型 6.MapReduce的编程规范 用户编 ...

  5. python循环,函数

    平常的网页会转换成ascll码,在转编译成十六进制a='http://www.mywebsit.com/?query=python&count=20'print(a)b=a.replace(' ...

  6. go学习笔记-函数

    函数 定义 格式 func function_name( [parameter list] ) [return_types] { 函数体 } 解析 func:函数由 func 开始声明 functio ...

  7. hdu 1394 Minimum Inversion Number(线段树)

    参考:http://blog.sina.com.cn/s/blog_691ce2b70101ldmm.html https://blog.csdn.net/wiking__acm/article/de ...

  8. 关于PHP性能提升踩过的一些坑

        性能这个东西,在网站规模到达一定程度后,会是一个永恒的主题.关于这方面,本人有一些拙见,现在拿出来,大家一起探讨下.     1.编码过程中,传递参数时,尽量少使用‘引用传参’.这是一个巨坑啊 ...

  9. Fragment保持状态切换

    在使用Activity管理多个Fragment时,每次切换Fragment使用的是replace,结果导致出现xxx is not currently in the FragmentManager异常 ...

  10. 2457: [BeiJing2011]双端队列

    2457: [BeiJing2011]双端队列 链接 很奇妙的转化. 题目要求最后的所有序列也是有序的,所以可以求出最后的序列(即排序后的序列),然后分成许多份,要求每一份都是一个双端序列,求最少分成 ...