题目

删除链表中等于给定值 val 的所有节点。

示例:

输入: 1->2->6->3->4->5->6, val = 6
输出: 1->2->3->4->5

解答

三种方法:

  • 双指针
  • 递归
  • 新开辟一个链表,增加空间复杂度

通过代码如下:

# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
# # 方法一:双指针。 时间复杂度O(n),空间复杂度O(1)
def removeElements(self, head: ListNode, val: int) -> ListNode:
thead = ListNode(-100)
thead.next = head
p, c = thead, head while c:
if c.val == val:
p.next = c.next
c = c.next
else:
p = c
c = c.next
return thead.next # # 方法三:
# # 最笨方法,新建一条链表存。时间复杂度O(n),空间复杂度O(n)
# def removeElements(self, head: ListNode, val: int) -> ListNode:
# thead = ListNode(-100)
# p = thead
# while head:
# if head.val != val:
# temp = ListNode(head.val)
# p.next = temp
# p = temp
# head = head.next
# return thead.next # # 方法二:递归
# # 回溯时,判断当前节点的值是不是val。 时间复杂度O(n),空间复杂度O(n)
# def removeElements(self, head: ListNode, val: int) -> ListNode:
# if not head:
# return head # head.next = self.removeElements(head.next, val)
# if head.val == val:
# return head.next
# return head

【Leetcode链表】移除链表元素(203)的更多相关文章

  1. 力扣(LeetCode)移除链表元素 个人题解

    删除链表中等于给定值 val 的所有节点. 这题粗看并不困难,链表的特性让移除元素特别轻松,只用遇到和val相同的就跳过,将指针指向下一个,以此类推. 但是,一个比较麻烦的问题是,当链表所有元素都和v ...

  2. Java实现 LeetCode 203 移除链表元素

    203. 移除链表元素 删除链表中等于给定值 val 的所有节点. 示例: 输入: 1->2->6->3->4->5->6, val = 6 输出: 1->2 ...

  3. [LeetCode] 203. 移除链表元素(链表基本操作-删除)、876. 链表的中间结点(链表基本操作-找中间结点)

    题目 203. 移除链表元素 删除链表中等于给定值 val 的所有节点. 题解 删除结点:要注意虚拟头节点. 代码 class Solution { public ListNode removeEle ...

  4. 【LeetCode】203.移除链表元素

    203.移除链表元素 知识点:链表:双指针 题目描述 给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回 新的头节点 . 示例 ...

  5. [LeetCode] Remove Linked List Elements 移除链表元素

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

  6. Leecode刷题之旅-C语言/python-203移除链表元素

    /* * @lc app=leetcode.cn id=203 lang=c * * [203] 移除链表元素 * * https://leetcode-cn.com/problems/remove- ...

  7. [LeetCode] Remove Nth Node From End of List 移除链表倒数第N个节点

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  8. [LeetCode] 19. Remove Nth Node From End of List 移除链表倒数第N个节点

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  9. LeetCode 82,考察你的基本功,在有序链表中删除重复元素II

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode专题的第51篇文章,我们来看LeetCode第82题,删除有序链表中的重复元素II(Remove Duplicates ...

  10. LeetCode(83): 删除排序链表中的重复元素

    Easy! 题目描述: 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2 输出: 1->2 示例 2: 输入: 1->1-&g ...

随机推荐

  1. Spring Cloud Consul综合整理

    该项目通过自动配置和Spring环境以及其他Spring编程模型习惯用法提供了Spring Boot应用程序的Consul集成. 通过一些简单的注释,您可以快速启用和配置应用程序内的通用模式,并使用基 ...

  2. 交叉熵-loss-理解

    参考链接: https://blog.csdn.net/tsyccnh/article/details/79163834

  3. HTML5中类jQuery选择器querySelector和querySelectorAll的使用

    支持的浏览IE8+,Firefox3.5+,Safari3.1+ Chrome和Opera 10+ 1.querySelector()方法接收一个选择符,返回第一个匹配的第一个元素,如果没有返回nul ...

  4. pytorch dataloader 取batch_size时候 出现bug

    1.RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0. Got 342 and 2 ...

  5. 洛谷P3306 随机数生成器

    题意:给你一个数列,a1 = x,ai = (A * ai-1 + B) % P,求第一个是t的是哪一项,或者永远不会有t. 解:循环节不会超过P.我们使用BSGS的思想,预处理从t开始跳√P步的,插 ...

  6. MAC中已有的虚拟环境在pycharm 中进行调用

    首先确定虚拟环境的路径: 打开工作环境配置文件,找到工作目录: 在pycharm中解释器中找到工作目录中对应的虚拟环境进行配置就可以了

  7. Linux字体美化实战(Fontconfig配置)(转)

    原文地址:http://www.jinbuguo.com/gui/linux_fontconfig.html 本文的主题是Linux环境下的字体美化,但是首先得要有字体,然后才能谈美化.所以第一件事就 ...

  8. 20190725-Silly

    $ \mathsf{You\ think\ about\ what\ you\ want\ because\ you're\ just\ alive}$ ——C418-Alive 我不能yuanlia ...

  9. R语言中如何使用最小二乘法

    R语言中如何使用最小二乘法 这里只是介绍下R语言中如何使用最小二乘法解决一次函数的线性回归问题.         代码如下: > x<-c(6.19,2.51,7.29,7.01,5.7, ...

  10. Vue.之.路由跳转

    Vue.之.路由跳转 在进行项目开发的过程中,需要使用路由进行跳转.如下: // 不带有参数,在页面上跳转到别的页面 1. this.$router.push('/login/init');  // ...