zoukankan      html  css  js  c++  java
  • 力扣—Remove Duplicates from Sorted List(删除排序链表中的重复元素)python实现

    题目描述:

    中文:

    给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。

    示例 1:

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

    示例 2:

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

    英文:

    Given a sorted linked list, delete all duplicates such that each element appear only once.

    Example 1:

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

    Example 2:

    Input: 1->1->2->3->3
    Output: 1->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 not head or not head.next:
                return head
            dummy=ListNode(0)
            dummy.next=head
            p=head
            while p.next:
                if p.next.val==p.val:
                    p.next=p.next.next
                else:
                    p=p.next
            return dummy.next

    题目来源:力扣

  • 相关阅读:
    数据压缩和归档
    数据持久化
    文件和目录的使用
    数据及数据处理
    data types
    string services
    logging模块
    指导
    比较两个NSDate类型的参数相差的时间差
    推送 iOS 10
  • 原文地址:https://www.cnblogs.com/spp666/p/11637758.html
Copyright © 2011-2022 走看看