zoukankan      html  css  js  c++  java
  • LeetCode:203. 移除链表元素

    1、题目描述

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

    示例:

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

    2、题解

    2.1、解法一

    # 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
            """
            node = head
            prev = head
            while node:
                if node.val == val:
                    if node == head:
                        head = node.next
                    else:
                        prev.next = node.next
                else:
                    prev = node
                node = node.next
    
            return head
    

      

  • 相关阅读:
    2018ddctf wp
    装饰器
    python作用域
    闭包
    迭代器
    ord() expected string of length 1, but int found
    pygm2安装问题
    elf逆向入门
    【POJ
    【POJ
  • 原文地址:https://www.cnblogs.com/bad-robot/p/10065695.html
Copyright © 2011-2022 走看看