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. 利用nginx使ftp可以通过http访问

    ./nginx 启动服务./nginx -s stop 关闭服务./nginx -s reload 重新加载配置文件 搭建nginx映射ftp服务:打开nginx的配置文件nginx.conf(位于n ...

  2. web前端总结面试问题<CSS&HTML问题>

    一个父元素div,一个未知宽度.高度的子元素div [上下左右居中方法总结] //1.position布局,position设为absolute,其他同情景一 2.display:table 父级元素 ...

  3. cors(Cross-origin resource sharing)跨域资源共享

    阮一峰老师的文章(http://www.ruanyifeng.com/blog/2016/04/cors.html)跨域资源共享详解和https://developer.mozilla.org/zh- ...

  4. 处理laravel表单提交默认将空值转为null的问题

    比如表单提交,如果我们提交了这个字段,但是这个字段为空字符串.在Laravel中会自动转义成Null. 处理这个问题,直到找到中间件\vendor\laravel\framework\src\Illu ...

  5. 【Js】Jquery遍历-each(function(e){})中的e和$(this)的区别

    $("selector").each(function(e){ console.log($(e)); console.log($(this)); console.log(e); c ...

  6. 强化记忆之php

    php 输出的区分 新手摸索道路,有说不对的地方,还请多多包涵. echo 能够输出一个以上的字符串,也能输出html标签 print  一次只能接受一个字符串(区分与echo),也能输出html标签 ...

  7. 什么是mysql数据库安全 简单又通俗的mysql库安全简介

    首先我们要了解一下什么是mysql数据库,mysql是目前网站以及APP应用上用的较多的一个开源的关系型数据库系统,可以对数据进行保存,分段化的数据保存,也可以对其数据进行检索,查询等功能的数据库. ...

  8. C语言Windows程序开发—Windows窗口样式与常用控件样式【第04天】

    (一)Windows窗口(MDICLIENT)样式介绍 /* Windows窗口样式 */ WS_BORDER //带有边框的窗口 WS_CAPTION //带有标题栏的窗口 WS_CHILD //子 ...

  9. YSZOJ:#247. [福利]可持久化线段树 (最适合可持久化线段树入门)

    题目链接:https://syzoj.com/problem/247 解题心得: 可持久化线段树其实就是一个线段树功能的加强版,加强在哪里呢?那就是如果一颗普通的线段树多次修改之后还能知道最开始的线段 ...

  10. Linux下中文乱码问题

    记录一下配置centos的时候遇到的一些常见问题 写了一个python脚本,有中文注释,而且会输出一些用户名称,其中包含中文字符.显示的时候出现乱码. 解决方案: 参见博客: Linux基础:中文显示 ...