描述

给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字。

示例 1:

输入: 1->2->3->3->4->4->5
输出: 1->2->5

示例 2:

输入: 1->1->1->2->3
输出: 2->3

Python

# 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:
        node0 = ranger = ListNode(0)
        node0.next = head
        self.duplicate(ranger,head)
        return node0.next
    
    def duplicate(self,node:ListNode,nodeNext:ListNode):
        while nodeNext and nodeNext.next:
            if nodeNext.val == nodeNext.next.val:
                while nodeNext.next and nodeNext.val == nodeNext.next.val:
                    nodeNext = nodeNext.next
                nodeNext = nodeNext.next
                node.next = nodeNext
            else:
                node = node.next
                nodeNext = nodeNext.next