删除给出链表中的重复元素(链表中元素从小到大有序),使链表中的所有元素都只出现一次
例如:
给出的链表为
,返回
.
给出的链表为
,返回
.
例如:
给出的链表为
给出的链表为
数据范围:链表长度满足
,链表中任意节点的值满足
进阶:空间复杂度
,时间复杂度 )
class Solution: def deleteDuplicates(self , head: ListNode) -> ListNode: # write code here New_head = p = ListNode(0) if head == None: return head while head: if head.next != None: if head.val != head.next.val: p.next = ListNode(head.val) p = p.next else: p.next = ListNode(head.val) p = p.next head = head.next return New_head.next