此题比#82 相对简单一些
# 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 head is None:
return head
H = ListNode()
H.next = head
p = H
q = H.next
r = H.next
while r.next:
r = r.next
if r.val == q.val:
if p == H:
H.next = r
p.next = r
q = r
else:
q = r
p = p.next
return H.next
网友评论