-
Notifications
You must be signed in to change notification settings - Fork 0
/
82.py
33 lines (28 loc) · 920 Bytes
/
82.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head:
return None
previous = ListNode(val=None, next=None)
dummy = previous
tmp = head.val
head = head.next
count = 1
while head:
if tmp != head.val:
if count == 1:
previous.next = ListNode(val=tmp, next=None)
previous = previous.next
tmp = head.val
count = 1
else:
count += 1
head = head.next
if count == 1:
previous.next = ListNode(val=tmp, next=None)
previous = previous.next
return dummy.next