题目:
给定一个已排序的链表的头 head
, 删除原始链表中所有重复数字的节点,只留下不同的数字 。返回 已排序的链表 。
代码:
# 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: Optional[ListNode]) -> Optional[ListNode]:
list1 = []
while head:
list1.append(head.val)
head = head.next
list1 = [k for k, v in Counter(list1).items() if v == 1]
head = point = ListNode()
for num in list1:
node = ListNode(num)
point.next = node
point = point.next
return head.next