此题设置双指针遍历链表,当前指针指向None叫停即可:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def deleteDuplicates(self, head: ListNode) -> ListNode:
if head==None or head.next==None:
return head
pre=head
be=head.next
while pre.next!=None:
if be.val==pre.val:
be=be.next
pre.next=be
else:
pre=pre.next
be=be.next
return head