203_Removed-Linked-List-Elements

Description

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

Example:

Input:  1->2->6->3->4->5->6, val = 6
Output: 1->2->3->4->5

Solution

Java solution 1

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

Runtime: 7 ms.

Java solution 2

Using dummy head node.

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

Runtime: 8 ms.

Python solution

# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
def removeElements(self, head, val):
"""
:type head: ListNode
:type val: int
:rtype: ListNode
"""
dummy_head = ListNode(-1)
dummy_head.next = head prev = dummy_head
while prev.next is not None:
if prev.next.val == val:
prev.next = prev.next.next
else:
prev = prev.next return dummy_head.next

Runtime: 88 ms. Your runtime beats 74.70 % of python3 submissions.

203_Removed-Linked-List-Elements的更多相关文章

  1. [LintCode] Remove Linked List Elements 移除链表元素

    Remove all elements from a linked list of integers that have value val. Have you met this question i ...

  2. Leetcode-203 Remove Linked List Elements

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

  3. 【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 ...

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

  5. 【LeetCode】203. Remove Linked List Elements

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

  6. 203. Remove Linked List Elements【easy】

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

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

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

  8. LeetCode_203. Remove Linked List Elements

    203. Remove Linked List Elements Easy Remove all elements from a linked list of integers that have v ...

  9. LeetCode--LinkedList--203. Remove Linked List Elements(Easy)

    203. Remove Linked List Elements(Easy) 题目地址https://leetcode.com/problems/remove-linked-list-elements ...

  10. 56-Remove Linked List Elements

    Remove Linked List Elements My Submissions QuestionEditorial Solution Total Accepted: 61924 Total Su ...

随机推荐

  1. 常用脚本--Kill所有连接到指定数据库上的回话

    USE [master] GO /****** Object: StoredProcedure [dbo].[Sp_KillAllProcessInDB] Script Date: 02/07/201 ...

  2. 如何在CentOS 7中禁用IPv6

    最近,我的一位朋友问我该如何禁用IPv6.在搜索了一番之后,我找到了下面的方案.下面就是在我的CentOS 7 迷你服务器关闭IPv6的方法. 你可以用两个方法做到这个. 方法 1 编辑文件/etc/ ...

  3. 大坑记录 - shell脚本删除操作

    背景 jenkins执行去执行shell命令,其中引用了一些jenkins的变量,如${WORKSPACE}这种,因为每次执行jenkins比较慢,于是想复制脚本出来想本地调试一下,直接复制了脚本过来 ...

  4. WPF圆角按钮

    <ControlTemplate x:Key="CornerButton" TargetType="{x:Type Button}"> <Bo ...

  5. Python3.5 学习六

    心灵鸡汤 电影推荐 末代独裁.杀戮战场.红色高棉.杀戮战场 面向对象介绍 class 类 object 对象 面向对象特性介绍 类的三大特性: 封装 继承 多态 类的构造函数 def __init__ ...

  6. String类的操作方法

    因String属于java核心包lang包的东西,所以不需要导包! /* * 字符串操作 * */ String name = "jck"; String name1 = &quo ...

  7. 脚本:定时释放 Linux/CentOS 缓存【转载自:杭州山不高】

    定时释放Linux/CentOS缓存的脚本(yl_dropcaches)如下: #!/bin/bash used=`free -m | awk 'NR==2' | awk '{print $3}'` ...

  8. GitHub创建项目,保存代码。

    平时学习会写一些代码,虽然只是零零散散的功能,但是基本都是在一个项目下操作,偶尔会忘记代码编辑顺序.国庆这几天在家,想把GitHub用起来,实现自己代码的可追溯,可查询.学习本篇博客,你需要一点的Gi ...

  9. java_对象序列化

    对象序列化(serializable) 序列化读:ObjectInputStream  ois=new ObjectInputStream(new FileInputStream("./gg ...

  10. redhat基本操作

     实验:安装redhat   需求:使用DVD镜像文件rhel-server-6.5-x86_64-dvd.iso,在虚拟机中安装RHEL 6系统 分区方案选择“使用所有空间”. 软件组选择“基本服务 ...