See Code
注意边界问题
O(n)
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def deleteDuplicates(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if not head:
return None
cur = head
pre = res = ListNode(0)
pre.next = head
while cur and cur.next:
if cur.next.val != cur.val:
pre = cur
cur = cur.next
else:
while cur.next and cur.val == cur.next.val:
cur = cur.next
pre.next = cur.next
cur = cur.next
return res.next