정렬 된 목록을 감안할 때, 각 요소는 한 번만 발생 그래서 모든 중복 요소를 삭제합니다.
예 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