# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def deleteDuplicates(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if not head or not head.next:
return head
dummyHead = ListNode(0)
dummyHead.next = head
pre = dummyHead
curNode = head
nextNode = curNode.next
curVal = curNode.val
unique = True
while curNode and nextNode:
if curNode.val == curVal and nextNode.val == curVal:
unique = False
curNode = nextNode
nextNode = nextNode.next
continue
if curNode.val == curVal and nextNode.val != curVal and not unique:
curNode = nextNode
nextNode = nextNode.next
curVal = curNode.val
unique = True
continue
else:
pre.next = curNode
pre = pre.next
curNode = nextNode
nextNode=nextNode.next
curVal = curNode.val
unique = True
if curNode.val == curVal and not unique:
pre.next = None
if curNode.val == curVal and unique:
pre.next = curNode
return dummyHead.next