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
    

      

  • 相关阅读:
    删数问题
    装箱问题
    活动选择
    智力大冲浪
    三国游戏
    最大乘积
    排队接水
    线段覆盖
    高精度重载运算符
    数的划分
  • 原文地址:https://www.cnblogs.com/bad-robot/p/10065695.html
Copyright © 2011-2022 走看看