LeetCode 82. 删除排序链表中的重复元素 II

2021-04-19 21:29

阅读:678

标签:__init__   重复元素   删除   else   head   lan   self   排序   return   

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

示例?1:
输入: 1->2->3->3->4->4->5
输出: 1->2->5

示例?2:
输入: 1->1->1->2->3
输出: 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:
        if head is None or head.next is None:
            return head 
        node = ListNode(None)
        node.next = head 
        pre = node 
        cur = head 
        while cur is not None and cur.next is not None:
            if cur.val == cur.next.val:
                while cur.next is not None and cur.val == cur.next.val:
                    cur = cur.next 
                pre.next = cur.next 
                cur = cur.next 
            else:
                cur = cur.next 
                pre = pre.next 
        return node.next 

LeetCode 82. 删除排序链表中的重复元素 II

标签:__init__   重复元素   删除   else   head   lan   self   排序   return   

原文地址:https://www.cnblogs.com/sandy-t/p/13287313.html


评论


亲,登录后才可以留言!