共计 451 个字符,预计需要花费 2 分钟才能阅读完成。
Given a sorted linked list, delete all duplicates such that each element appear only once.
Example 1:
Input: 1->1->2
Output: 1->2
Example 2:
Input: 1->1->2->3->3
Output: 1->2->3
解法:
# 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:
head_bak=head
while head_bak:
tmp=head_bak.next
while tmp :
if tmp.val==head_bak.val:
tmp=tmp.next
else:
break
head_bak.next=tmp
head_bak=tmp
return head
正文完
请博主喝杯咖啡吧!