• Leetcode 203. Remove Linked List Elements


    Description: Remove all elements from a linked list of integers that have value val.

    Link: https://leetcode.com/problems/remove-linked-list-elements/

    Example:

    Input:  1->2->6->3->4->5->6, val = 6
    Output: 1->2->3->4->5

    思路: 删除单链表中元素值为val 的节点,遍历一遍,遇到就删除。

    class Solution(object):
        def removeElements(self, head, val):
            """
            :type head: ListNode
            :type val: int
            :rtype: ListNode
            """
            if not head: return None
            rh = ListNode(0)
            rh.next = head
            pre = rh
            p = head
            while p:
                if p.val == val:
                    pre.next = p.next
                else:
                    pre = p
                p = p.next
            return rh.next

    日期: 2020--12-1  时间过慢一点呀

  • 相关阅读:
    django rest_framework中将json输出字符强制为utf-8编码
    Java
    Java
    Oracle
    IDEA
    Ubuntu
    Ubuntu
    Ubuntu
    Ubuntu
    Oracle
  • 原文地址:https://www.cnblogs.com/wangyuxia/p/14068199.html
Copyright © 2020-2023  润新知