当前位置:网站首页>学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
边栏推荐
- The shooting range that web penetration must play - DVWA shooting range 1 (centos8.2+phpstudy installation environment)
- MOMENTUM: 2 vulnhub walkthrough
- redis未授权访问(4-unacc)
- Praying: 1 vulnhub walkthrough
- easyswoole uses redis to perform geoRadiusByMember Count invalid fix
- What are the PHP framework?
- CTF-网鼎杯往届题目
- [league/climate] A robust command-line function manipulation library
- [mikehaertl/php-shellcommand] A library for invoking external command operations
- Masashi: 1 vulnhub walkthrough
猜你喜欢

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

动力:2 vulnhub预排

Phonebook

(5) 模块与包、编码格式、文件操作、目录操作

CTF之xxe

MOMENTUM: 2 vulnhub walkthrough

Praying: 1 vulnhub walkthrough

hackmyvm-hopper walkthrough

What are the killer super powerful frameworks or libraries or applications for PHP?

(3)Thinkphp6数据库
随机推荐
DVWA drone installation tutorial
CSRF(跨站请求伪造)
17. JS conditional statements and loops, and data type conversion
(2) Sequence structures, Boolean values of objects, selection structures, loop structures, lists, dictionaries, tuples, sets
Praying: 1 vulnhub walkthrough
C language uses stack to calculate infix expressions
hackmyvm: juggling walkthrough
vim edit mode
PHP反序列化漏洞
hackmyvm-hopper预排
1. Beginning with PHP
CTF introductory notes ping
Several interesting ways to open PHP: from basic to perverted
c语言用栈实现计算中缀表达式
战场:3(双子叶植物)vulnhub走读
(8) requests, os, sys, re, _thread
What are the PHP framework?
easyswoole uses redis to perform geoRadiusByMember Count invalid fix
Smart Tips for Frida Scripting in Kali Environment
PHP deserialization vulnerability