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. 提问的智慧——其实你真的不会提问!(转)

    在黑客世界里,当提出一个技术问题时,你能得到怎样的回答?这取决于挖出答案的难度,同样取决于你提问的方法.本指南旨在帮助你提高发问技巧,以获取你最想要的答案.       首先你必须明白,黑客们只偏爱艰 ...

  2. [Erlang20]一起攻克Binary

    第一次看到Joe Armstong的<Erlang 程序设计>里面对Binary的描述时,觉得这个东西好复杂,语法这么奇特(我觉得是Erlang语法中最不好懂的部分); 然后在项目中:Bi ...

  3. Solr相似度算法三:DRFSimilarity

    该Similarity 实现了  divergence from randomness (偏离随机性)框架,这是一种基于同名概率模型的相似度模型. 该 similarity有以下配置选项: basic ...

  4. mac下MySQL、MysqL workbench的安装与配置配置

    请戳这里 但是3.4的创建instance还不会

  5. JAVA中Date类的使用

    一. Date类 Date类对象的创建: 1.创建一个当前时间的Date对象 //创建一个代表系统当前日期的Date对象 Date d = new Date(); 2.创建一个我们指定的时间的Date ...

  6. 关于Collections的操作方法

    Collections是一个包装类,其中包含有各种有关集合操作的静态多态方,比如可以作用在List和Set上,此类不能实例化. 排序 Integer[] array = new Integer[]{3 ...

  7. LOJ#2015. 「SCOI2016」妖怪(凸包)

    传送门 首先可以把每个妖怪看成二维平面上的一个点,那么每一个环境\((a,b)\)就可以看成一条斜率\(k=-\frac{b}{a}\)的过该点的直线,战斗力就是这条直线在两坐标轴上的截距之和 对于每 ...

  8. PHP中define()和dirname(__FILE__)

    1,define() 函数定义一个常量.常量类似变量,不同之处在于: (1)在设定以后,常量的值无法更改 (2)常量名不需要开头的美元符号 ($) (3)作用域不影响对常量的访问 (4)常量值只能是字 ...

  9. redis cluster 的ERR max number of clients reached 问题排查

    早上发现微服务连不上redis cluster了,看来下日志如下 [root@win-jrh378d7scu 7005]# bin/redis-cli -c -h 15.31.213.183 -p 7 ...

  10. React 初识

    React We built React to solve one problem: building large applications with data that changes over t ...