码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-10-08 21:55:25      阅读:86      评论:0      收藏:0      [点我收藏+]

标签:efi   sel   delete   type   class   let   ret   rom   ica   

题目描述:

中文:

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

示例 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

题目来源:力扣

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

标签:efi   sel   delete   type   class   let   ret   rom   ica   

原文地址:https://www.cnblogs.com/spp666/p/11637758.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!