zoukankan      html  css  js  c++  java
  • 【leetcode❤python】 203. Remove Linked List Elements

    # Definition for singly-linked list.
    # class ListNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None

    class Solution(object):
        def removeElements(self, head, val):
            """
            :type head: ListNode
            :type val: int
            :rtype: ListNode
            """
            if head==None:return []
            dummy=ListNode(-1)
            dummy.next=head
            p=dummy
            
            while head:
                
                if head.val==val:
                    p.next=head.next

                    #!!!写的时候一直报错,是因为没有把head节点替换,删除节点时一定要将删除节点替换掉。
                    head=p
                
                p=head
                head=head.next
                
                
            return dummy.next

  • 相关阅读:
    一行代码搞定图片缩放、旋转、加水印
    如何学习 Webpack
    Webpack 概念
    Webpack 入门
    asp.net core教程 (一)
    asp.net core教程 (二)
    ap.net core 教程(三)
    Grafana 安装配置
    zabbix-3.0.x LTS源码安装配置
    MariaDB Security
  • 原文地址:https://www.cnblogs.com/kwangeline/p/6016912.html
Copyright © 2011-2022 走看看