题目:
给定一个已排序的链表的头
head
, 删除原始链表中所有重复数字的节点,只留下不同的数字 。返回 已排序的链表 。来源:力扣(LeetCode)
链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台
示例:
示例 1:
输入:head = [1,2,3,3,4,4,5]
输出:[1,2,5]
示例 2:输入:head = [1,1,1,2,3]
输出:[2,3]
解法:
转成列表操作。
代码:
# 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