83. Remove Duplicates from Sorted List

2,574次阅读
没有评论

共计 451 个字符,预计需要花费 2 分钟才能阅读完成。

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:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def deleteDuplicates(self, head: ListNode) -> ListNode:
        head_bak=head
        while head_bak:
            tmp=head_bak.next
            while tmp :
                if tmp.val==head_bak.val:
                    tmp=tmp.next
                else:
                    break
                
            head_bak.next=tmp
            head_bak=tmp
        return head
正文完
请博主喝杯咖啡吧!
post-qrcode
 
admin
版权声明:本站原创文章,由 admin 2019-02-23发表,共计451字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
验证码