# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
#
#
# @param head ListNode类
# @return ListNode类
#
class Solution:
def deleteDuplicates(self , head ):
# write code here
#input: head of linked list
# output: deduplicated linkedlist
# idea: two pointer
# 1. slow: store previous node of the first duplicated node
# 2. fast store the last duplicated node
# 3.so we need fake_node and start from fake node so that we can
# easily remove the head node if necessary
# 4. iterate the whole list using fast
# 5. when we find fast.val == fast.next.val
# use temp = fast.next and find the end of the duplicated string
# then let slow.next = temp, fast = temp to skip duplicated elements
#bsae case:
if not head:
return head
slow = fake_head = ListNode(None)
fake_head.next = head
fast = head
while fast and fast.next:
if fast.val == fast.next.val:
cur = fast.next
while cur and cur.val == fast.val:
cur = cur.next
slow.next = cur
fast = cur
else:
slow = slow.next
fast = fast.next
return fake_head.next