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. pom.xml文件说明(八)

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...

  2. 【oracle】 months_between(date1,date2)

    (20090228,20080228)====12 (20090228,20080229)====12 (20080229,20070228)====12 (20100331,20100228)=== ...

  3. Java基础的容错

    新手会有一些常犯的过失,一般一个新手在学习Java开发的时分,往往会挑选买书去学习,首要这样的学习功率是非常差的,比如在学习html,css的时分,是彻底不必看书的.书里大多数东西你都不了解.这是新手 ...

  4. js将字符串内空格去除的方法

    function noSpace(x){ if(x.match(/\s*/g)){ return x.replace(/\s*/g,""); }else{ return x; } ...

  5. js对url进行编码的方法(encodeURI和 encodeURICompoent())

    encodeURI(): 对整个URL进行编码,对应的解码方式:decodeURI() encodeURIComponent() : 对查询字符串进行编码,对应的解码方式:decodeURICompo ...

  6. 洛谷 P1440 求m区间内的最小值

    传送门 思路 由于数据范围很大,所以使用单调队列,和滑动窗口这道题类似 首先第一个数输出\(0\),因为第一个数之前没有数 然后通过样例我们发现,最后一个数并没有派上什么用场,所以循环\(n-1\)轮 ...

  7. Linux性能优化实战学习笔记:第十五讲

    一.内存映射 内存管理也是操作系统最核心的功能之一,内存主要用来存储系统和应用程序的指令.数据.缓存等 1.我们通说的内存指的是物理内存还是虚拟内存? 我们通常说的内存容量,其实这指的是物理内存,物理 ...

  8. [LeetCode] 896. Monotonic Array 单调数组

    An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is mono ...

  9. [LeetCode] 321. Create Maximum Number 创建最大数

    Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...

  10. .NET Core:跨域

    在Startup中的ConfigureServices方法中配置:services.AddCors(options => options.AddPolicy("any", b ...