Given a sorted list, delete all the duplicate elements, so that each element occurs only once.
Example 1:
Input: 1-> 1-> 2
Output: 1-> 2
Example 2:
Input: 1-> 1-> 2-> 3-> 3
Output: 1-> 2-> 3
Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/remove-duplicates-from-sorted-list
Mainly [1,1,1] [1,1,1,2] this case three
# 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