(파이썬) 알고리즘 개 요리 일기 (목록 시리즈) 83 삭제가 반복 요소의 목록을 정렬 _leetcode

정렬 된 목록을 감안할 때, 각 요소는 한 번만 발생 그래서 모든 중복 요소를 삭제합니다.

예 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]이 경우 세

# 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