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的更多相关文章

  1. LeetCode(203) Remove LinkedList Elements

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

  2. LeetCode 203:移除链表元素 Remove LinkedList Elements

    删除链表中等于给定值 val 的所有节点. Remove all elements from a linked list of integers that have value val. 示例: 输入 ...

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

  4. 203. Remove Linked List Elements【easy】

    203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...

  5. 203. Remove Linked List Elements - LeetCode

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

  6. 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 -- ...

  7. 203. Remove Linked List Elements

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

  8. (easy)LeetCode 203.Remove Linked List Elements

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

  9. Java [Leetcode 203]Remove Linked List Elements

    题目描述: Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> ...

  10. [LeetCode] 203. Remove Linked List Elements 解题思路

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

随机推荐

  1. Spring Boot2.0+中,自定义配置类扩展springMVC的功能

    在spring boot1.0+,我们可以使用WebMvcConfigurerAdapter来扩展springMVC的功能,其中自定义的拦截器并不会拦截静态资源(js.css等). @Configur ...

  2. zz目标检测

    deep learning分类 目标检测-HyperNet-论文笔记 06-06 基础DL模型-Deformable Convolutional Networks-论文笔记 06-05 基础DL模型- ...

  3. 洛谷 U86501 趣味擂台

    洛谷 U86501 趣味擂台 题目传送门 题目背景 \(JDFZ\)\(2019\)秋季运动会开始辣!运动会中有一个叫做"趣味擂台"的游戏...... 题目描述 游戏内容是这样的: ...

  4. C++面向对象程序设计学习笔记(2)

    C++在非面向对象方面的扩充 C++向下兼容C语言,因此C语言中的语法在C++中依然成立 输入输出 C++添加了标准输入流对象cin和标准输出流对象cout来进行输入输出, 例: #include&l ...

  5. 【51Nod1555】布丁怪

    [51Nod1555]布丁怪 题面 51Nod 题目大意: 给你一个\(n\times n\)的棋盘以及\(n\)个棋子,每个棋子坐标为\((x_i,y_i)\),保证棋盘的每一行或一列都有且仅有一个 ...

  6. B1047 编程团体赛 (20 分)

    一.参考代码 #include<iostream> #include<cstring> using namespace std; int hashTable[1010]; in ...

  7. [LeetCode] 464. Can I Win 我能赢吗

    In the "100 game," two players take turns adding, to a running total, any integer from 1.. ...

  8. 【贪心】umi的函数

    原题传送门 思路 这道题乍一看很难,然而实际在草纸上一模拟,结果就出来了. 分析:这道题实际上要么无解,要么有无数个解,因而题目只要求输出了一个解(明白这点题目就做出来一半了). 题中,规定所求z满足 ...

  9. uniApp配置文件几个注意点

    虽然有文档,但是偶尔还是会又找不到的,写下来遇到过的问题,随时补充.好记性不如烂笔头. 1.打包完安装之后,app 有时候会弹出一个提示框.如下: 修改配置项,设置 ignoreVersion 为 t ...

  10. Circumference of circle

    public class Solution { public static void main(String[] args) { Scanner ip = new Scanner(System.in) ...