83. Remove Duplicates from Sorted List

924次阅读
没有评论
Given a sorted linked list, delete all duplicates such that each element appear only once.

Example 1:

<strong>Input:</strong> 1->1->2
<strong>Output:</strong> 1->2

Example 2:

<strong>Input:</strong> 1->1->2->3->3
<strong>Output:</strong> 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
版权声明:本站原创文章,由admin2019-02-23发表,共计451字。
转载提示:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)