83. Remove Duplicates from Sorted List

1,795次阅读
没有评论
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
admin
版权声明:本站原创文章,由 admin 2019-02-23发表,共计451字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)