(パイソン)アルゴリズム犬の料理日記(リストシリーズ)_leetcode 83.削除は、繰り返し要素のリストをソート

ソートされたリストを考えると、各要素が1回だけ発生だから、すべての重複要素を削除します。

例1:

入力:1 - > 1 - > 2
出力:1 - > 2
例2:

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

出典:滞在ボタン(LeetCode)
リンクします。https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list

主[1,1,1] [1,1,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:
        cur = head
        while(head and head.next):
            if head.val == head.next.val:
                head.next = head.next.next
            # if head.next:
            #      if head.val != head.next.val:
            #          head = head.next
            # else:
            #     head = head.next
            else:
                head = head.next
        return cur


 

公開された44元の記事 ウォンの賞賛0 ビュー1908

おすすめ

転載: blog.csdn.net/weixin_39331401/article/details/104558491