zoukankan      html  css  js  c++  java
  • LeetCode--203--删除链表中的节点

    问题描述:

    删除链表中等于给定值 val 的所有节点。

    示例:

    输入: 1->2->6->3->4->5->6, val = 6
    输出: 1->2->3->4->5
    

    方法1:防止[1,1,1,1] 1 用while head。

     1 class Solution(object):
     2     def removeElements(self, head, val):
     3         """
     4         :type head: ListNode
     5         :type val: int
     6         :rtype: ListNode
     7         """
     8         if head == None:
     9             return 
    10         while head and  head.val == val:
    11             head = head.next
    12         p = head
    13         while p:
    14             if p.next and p.next.val == val:
    15                 p.next = p.next.next
    16         
    17             else:
    18                 p = p.next
    19         return head

    2018-09-17 19:34:21

  • 相关阅读:
    ch_6802 車的放置
    ch_POJ2182 Lost Cows
    ch_4201 楼兰图腾
    luogu_P3368【模板】树状数组 2
    门面
    建造者
    模板方法
    状态
    抽象工厂
    工厂方法
  • 原文地址:https://www.cnblogs.com/NPC-assange/p/9664377.html
Copyright © 2011-2022 走看看