# 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]:
""" tc O(N) sc O(1)
"""
if not head or not head.next: return head
dummy = ListNode(0,head)
pre = dummy
cur = head
while cur:
# if there are duplicate, keep moving cur until last point of duplicates
while cur.next and cur.val == cur.next.val:
cur = cur.next
if pre.next == cur: # there is no duplicate between pre and cur
pre = pre.next
else:
pre.next = cur.next # keep pre still, temtatively try pre's next to first non duplicate node
cur = cur.next
return dummy.next