classSolution:defremoveDuplicates(self,nums: List[int]) ->int: i =0for num in nums:if i <2or num != nums[i -2]:# i标记的是下一个可以交换的位置, 不重复超过两个 nums[i]= num i +=1return i
classSolution:defdeleteDuplicates(self,head: Optional[ListNode]) -> Optional[ListNode]: dummy =ListNode(-1, next=head) pre = dummy while pre.next: cur = pre.next while cur.next and cur.val == cur.next.val: cur = cur.nextif pre.next != cur: pre.next = cur.nextelse: pre = pre.nextreturn dummy.next