-
Notifications
You must be signed in to change notification settings - Fork 26
/
83.RemoveDuplicatesfromSortedList.py
40 lines (36 loc) · 1.13 KB
/
83.RemoveDuplicatesfromSortedList.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
33
34
35
36
37
38
39
40
"""
Given a sorted linked list, delete all duplicates such that each element
appear only once.
Example:
Input: 1->1->2->3->3
Output: 1->2->3
"""
#Difficulty: Easy
#165 / 165 test cases passed.
#Runtime: 40 ms
#Memory Usage: 13.9 MB
#Runtime: 40 ms, faster than 87.68% of Python3 online submissions for Remove Duplicates from Sorted List.
#Memory Usage: 13.9 MB, less than 31.86% of Python3 online submissions for Remove Duplicates from Sorted List.
# 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: ListNode) -> ListNode:
if not head:
return
prev = head
curr = head.next
while curr:
if prev.val != curr.val:
prev = curr
curr = curr.next
else:
while prev.val == curr.val:
curr = curr.next
if not curr:
prev.next = None
break
prev.next = curr
return head