• 力扣 ——Remove Duplicates from Sorted List II(删除排序链表中的重复元素 II)python实现


    题目描述:

    中文:

    给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字。

    示例 1:

    输入: 1->2->3->3->4->4->5
    输出: 1->2->5

    示例 2:

    输入: 1->1->1->2->3
    输出: 2->3

    英文:

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.

    Example 1:

    Input: 1->2->3->3->4->4->5
    Output: 1->2->5

    Example 2:

    Input: 1->1->1->2->3
    Output: 2->3

    # Definition for singly-linked list.
    # class ListNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution(object):
        def deleteDuplicates(self, head):
            """
            :type head: ListNode
            :rtype: ListNode
            """
            if head ==None or head.next == None:
                return head
            dummy = ListNode(0)
            dummy.next = head
            p = dummy
            tmp = dummy.next
            while p.next:
                while tmp.next and tmp.next.val == p.next.val:
                    tmp = tmp.next
                if tmp == p.next:
                    p = p.next
                    tmp = p.next
                else:
                    p.next = tmp.next
            return dummy.next

    题目来源:力扣

  • 相关阅读:
    java内存分析 栈 堆 常量池的区别
    了解struts2 action的一些原理
    条件语句的写法
    Library中的title与Name
    样式优先级、margin
    文件夹IsShow字段为空
    Ispostback
    HierarchicalDataBoundControl 错误
    DBNull与Null
    sharepoint中的YesNo字段
  • 原文地址:https://www.cnblogs.com/spp666/p/11644330.html
Copyright © 2020-2023  润新知