题目

删除链表中等于给定值 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. PAT甲级——A1026 Table Tennis

    A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For a ...

  2. javascript基础:语法与html结合方式

    一.基本语法: 1.与html结合方式 1.内部JS: *  定义<script>,标签体内容就是JS代码 2.外部JS: *  定义<script>,通过src属性引入外部的 ...

  3. Leetcode144. Binary Tree Preorder Traversal二叉树的前序遍历

    给定一个二叉树,返回它的 前序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 递归: class S ...

  4. 跟我一起了解koa(三)

    跟我一起了解koa中间件 一.是 什么是 Koa 的中间件 通俗的讲: :中间件就是匹配路由之前或者匹配路由完成做的一系列的操作,我们就可以 把它叫做中间件. 在 在express件 中间件( (Mi ...

  5. localStorage对象简单应用 - - 访问次数

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. 读书笔记--Head First PMP目录

    1.引言 2.阻止.约束和项目 3.过程框架 4.项目整合管理 5.范围管理 6.时间管理 7.成本管理 8.质量管理 9.人力资源管理 10.沟通管理 11.项目风险管理 12.采购管理 13.职业 ...

  7. vue前后端分离

    axios前后端交互 安装 一定要安装到`项目目录下 cnpm install axios 配置 在main.js中配置 //配置axios import axios from 'axios' Vue ...

  8. P5562 [Celeste-B]Center of the Earth 题解

    构造 因为题目只要求两位相同,所以可以暴力枚举这两位所有的可能性,方案数为\(O(n^2)\). 但是,这么做是显然不优的,因为完全没有用到第三位. 观察题目条件:n为偶数. 就想一想能不能奇数偶数分 ...

  9. Leetcode91.Decode Ways解码方法

    一条包含字母 A-Z 的消息通过以下方式进行了编码: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 给定一个只包含数字的非空字符串,请计算解码方法的总数. 示例 1 ...

  10. 初探element+vue+vue-router

    基本步骤先准备好 npm install -g vue-cli npm init webpack cnpm i element-ui -S 修改/src/main.js // The Vue buil ...