203--Remove LinkedList Elements
package LinedList;
public class RemoveLinkedListElements {
//解法一:循环
public ListNode removeElements(ListNode head, int val) {
while (head!=null&&head.val==val){
ListNode temp=head;
head=temp.next;
temp.next=null;
}
if (head==null)
return null;
ListNode previous=head;
while (previous.next!=null){
if (previous.next.val==val){
ListNode temp=previous.next;
previous.next=temp.next;
temp.next=null;
}
else{
previous=previous.next;
}
}
return head;
}
//解法二,使用虚拟头节点的循环。
public ListNode removeElements2(ListNode head, int val) {
ListNode dummyHead=new ListNode(-1);
dummyHead.next=head;
ListNode previous=dummyHead;
while (previous.next!=null){
if (previous.next.val==val){
ListNode temp=previous.next;
previous.next=temp.next;
temp.next=null;
} else {
previous=previous.next;
}
}
return dummyHead.next;
}
//解法三:使用递归。
public ListNode removeElements3(ListNode head, int val) {
if(head==null)
return null;
head.next=removeElements(head.next,val);
return head.val==val?head.next:head;
}
}
203--Remove LinkedList Elements的更多相关文章
- LeetCode(203) Remove LinkedList Elements
题目 Remove all elements from a linked list of integers that have value val. Example Given: 1 –> 2 ...
- LeetCode 203:移除链表元素 Remove LinkedList Elements
删除链表中等于给定值 val 的所有节点. Remove all elements from a linked list of integers that have value val. 示例: 输入 ...
- 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题要求 ...
- 203. Remove Linked List Elements【easy】
203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...
- 203. Remove Linked List Elements - LeetCode
Question 203. Remove Linked List Elements Solution 题目大意:从链表中删除给定的数 思路:遍历链表,如果该节点的值等于给的数就删除该节点,注意首节点 ...
- Java for LeetCode 203 Remove Linked List Elements
Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...
- 203. Remove Linked List Elements
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- (easy)LeetCode 203.Remove Linked List Elements
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- Java [Leetcode 203]Remove Linked List Elements
题目描述: Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> ...
- [LeetCode] 203. Remove Linked List Elements 解题思路
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
随机推荐
- numpy cookbook
1.第一章 import numpy as np import matplotlib.pyplot as plt import scipy import PIL import scipy.misc l ...
- 【day03】php
一.类型判别函数库 1.安装:类型判别函数库是PHPCORE的组成部分,不用安装 2. (1)is_integer|is_int|is_long 描述: 检测变量是否是整数 格式: ...
- [LeetCode] 57. Insert Interval 插入区间
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- 十大基础排序算法[java源码+动静双图解析+性能分析]
一.概述 作为一个合格的程序员,算法是必备技能,特此总结十大基础排序算法.java版源码实现,强烈推荐<算法第四版>非常适合入手,所有算法网上可以找到源码下载. PS:本文讲解算法分三步: ...
- intellij idea安装教程
1. 双击ideaIU-12.1.1.exe,点击下一步,安装目录改为d:\Program Files\JetBrains\IntelliJ IDEA,其他项都默认即可(此处更改目录方便重做系统,不用 ...
- java web开发入门五(ssh整合)基于intellig idea
SSH整合 1.引入jar包 Struts 核心jar Hibernate 核心jar Spring Core 核心功能 Web 对web模块支持 Aop aop支持 Orm 对hiber ...
- Java中HashMap和TreeMap的区别
什么是Map集合在数组中我们是通过数组下标来对其内容索引的,而在Map中我们通过对象来对对象进行索引,用来索引的对象叫做key,其对应的对象叫做value.这就是我们平时说的键值对. HashMap ...
- FontForge:免费字库设计软件 附使用教程
引用:http://www.sucaijishi.com/2018/articles_0817/259.html 如何设计一套自己的字库?今天分享一个开源的字库设计软件FontForge, 官方下载: ...
- why’s kafka so fast
As we all know that Kafka is very fast, much faster than most of its competitors. So what’s the reas ...
- 只安装自己需要的 Office 2016 组件的方法
以往Office的安装包都采用的是MSI安装器,允许用户在安装时选择安装的组件,但是微软发布的Office 2016安装包只提供了C2R(ClickToRun)方式,因此默认情况下用户无法选择安装组件 ...