当前位置:网站首页>学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
边栏推荐
- Phpstudy安装Thinkphp6(问题+解决)
- The CTF introduction of PHP file contains
- 2. PHP variables, output, EOF, conditional statements
- What will be new in PHP8.2?
- 13. JS output content and syntax
- [symfony/finder] The best file manipulation library
- (8) requests, os, sys, re, _thread
- PHP Foundation March Press Announcement Released
- Cookie is used to collect the admin privileges CTF foundation problem
- hackmyvm: juggling walkthrough
猜你喜欢
c语言用栈实现计算中缀表达式
Several interesting ways to open PHP: from basic to perverted
hackmyvm-hopper预排
攻防世界—MISC 新手区1-12
(2)Thinkphp6模板引擎**标签
What are the killer super powerful frameworks or libraries or applications for PHP?
ES6 array extension methods map, filter, reduce, fill and array traversal for…in for…of arr.forEach
Eric target penetration test complete tutorial
CTF入门笔记之ping
Shuriken: 1 vulnhub walkthrough
随机推荐
Cookie is used to collect the admin privileges CTF foundation problem
File upload vulnerability
Phpstudy installs Thinkphp6 (problem + solution)
(6) Design of student information management system
PHP反序列化漏洞
(2)Thinkphp6模板引擎**标签
CTF入门笔记之SQL注入
About the apache .htaccess file of tp
A network security guinea pig's learning path - scripting of advanced usage of nmap
hackmyvm: again walkthrough
ES6 three-dot operator, array method, string extension method
C language uses stack to calculate infix expressions
Xiaoyao multi-open emulator ADB driver connection
Sensitive information leakage
Orasi: 1 vulnhub walkthrough
3. PHP data types, constants, strings and operators
PHP image compression to specified size
Alibaba Cloud MySQL 5.7 installation and some major problems (total)
敏感信息泄露
GreenOptic: 1 vulnhub walkthrough