当前位置:网站首页>学IT,找工作——移除链表元素
学IT,找工作——移除链表元素
2022-08-02 03:25:00 【PenguinLeee】
203. 移除链表元素
给定一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回:新的头节点 。
解:
需要考虑链表头节点可能出现的情况:
- 链表头节点可能需要删除也可能不需要删除
- 可能是空链表
综上,设计一个虚拟的节点 vhead 放在给定 head 之前。代码如下:
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def removeElements(self, head, val):
""" :type head: ListNode :type val: int :rtype: ListNode """
if head == None:
return None
vhead = ListNode(next=head)
traverse = vhead
while 1:
if traverse.next == None:
break
if traverse.next.val == val:
traverse.next = traverse.next.next
else:
traverse = traverse.next
return vhead.next
边栏推荐
- ES6 three-dot operator, array method, string extension method
- PHP image compression to specified size
- (1) introduction to Thinkphp6, installation view, template rendering, variable assignment
- Eric靶机渗透测试通关全教程
- hackmyvm: again walkthrough
- 利用cookie获取admin权限 CTF基础题
- After the mailbox of the Pagoda Post Office is successfully set up, it can be sent but not received.
- SQL classification, DQL (Data Query Language), and corresponding SQL query statement demonstration
- ES6 array extension methods map, filter, reduce, fill and array traversal for…in for…of arr.forEach
- About the apache .htaccess file of tp
猜你喜欢

CTF入门笔记之SQL注入
![[league/climate] A robust command-line function manipulation library](/img/ce/39114b1c74af649223db97e5b0e29c.png)
[league/climate] A robust command-line function manipulation library

Stable and easy-to-use short connection generation platform, supporting API batch generation

hackmyvm-random walkthrough

Praying: 1 vulnhub walkthrough

PHP deserialization vulnerability

(4) Function, Bug, Class and Object, Encapsulation, Inheritance, Polymorphism, Copy

13. JS output content and syntax

OPENSSL基本实验以及OPENSSL详解

How to log in to Alibaba Cloud server using the admin account
随机推荐
阿里云MySQL5.7安装以及部分主要问题(总和)
(8) requests, os, sys, re, _thread
14. JS Statements and Comments, Variables and Data Types
hackmyvm-bunny walkthrough
uniapp | Compilation error after updating with npm update
hackmyvm: kitty walkthrough
WeChat applet development video loading: [Rendering layer network layer error] Failed to load media
DarkHole: 2 vulnhub walkthrough
Shuriken: 1 vulnhub walkthrough
Solve the problem of Zlibrary stuck/can't find the domain name/reached the limit, the latest address of Zlibrary
4. The form with the input
The shooting range that web penetration must play - DVWA shooting range 1 (centos8.2+phpstudy installation environment)
(5) 模块与包、编码格式、文件操作、目录操作
(1) the print () function, escape character, binary and character encoding, variables, data type, the input () function, operator
What will be new in PHP8.2?
(3) string
Several interesting ways to open PHP: from basic to perverted
ES6 array extension methods map, filter, reduce, fill and array traversal for…in for…of arr.forEach
[league/climate] A robust command-line function manipulation library
DNS详解