此题细节太多,非常适合用test-case driven的方法做题
# 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
flag = False
while r.next:
r = r.next
if r.val != q.val:
q = q.next
if flag:
if p == H:
H.next = q
p.next = q
flag = False
else:
p = p.next
else:
flag = True
q = q.next
if flag:
p.next = None
return H.next
```
网友评论