当前位置:网站首页>学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
边栏推荐
- CSRF(跨站请求伪造)
- hackmyvm-random walkthrough
- Phonebook
- redis未授权访问(4-unacc)
- A network security guinea pig's learning path - scripting of advanced usage of nmap
- hackmyvm: again walkthrough
- Alfa: 1 vulnhub walkthrough
- By figure, a (complete code at the end)
- Orasi: 1 vulnhub walkthrough
- CSRF (Cross Site Request Forgery)
猜你喜欢
(6) Design of student information management system
Introduction to PHP (self-study notes)
ES6 three-dot operator, array method, string extension method
The shooting range that web penetration must play - DVWA shooting range 1 (centos8.2+phpstudy installation environment)
hackmyvm: may walkthrough
CTF之xxe
(2)Thinkphp6模板引擎**标签
hackmyvm: juggling walkthrough
(7) 浅学 “爬虫” 过程 (概念+练习)
IO stream, encoding table, character stream, character buffer stream
随机推荐
Stable and easy-to-use short connection generation platform, supporting API batch generation
The roll call system and array elements find maximum and minimum values for sorting of objects
(3) 字符串
hackmyvm: kitty walkthrough
C language uses stack to calculate infix expressions
The shooting range that web penetration must play - DVWA shooting range 1 (centos8.2+phpstudy installation environment)
CSRF (Cross Site Request Forgery)
Using PHPMailer send mail
Summary of php function vulnerabilities
file contains vulnerabilities
hackmyvm-hopper预排
(1) print()函数、转义字符、二进制与字符编码 、变量、数据类型、input()函数、运算符
c语言用栈实现计算中缀表达式
一次代码审计的笔记(CVE-2018-12613 phpmyadmin文件包含漏洞)
(6) Design of student information management system
(1) introduction to Thinkphp6, installation view, template rendering, variable assignment
hackmyvm-hopper walkthrough
The CTF introductory notes of SQL injection
The CTF introduction of PHP file contains
hackmyvm: juggling walkthrough