📌  相关文章
📜  用于从排序链表中删除所有重复项的Python程序

📅  最后修改于: 2022-05-13 01:56:56.312000             🧑  作者: Mango

用于从排序链表中删除所有重复项的Python程序

给定一个排序的链表,删除所有重复数字(所有出现)的节点,只留下在原始列表中出现一次的数字。
例子:

Input: 23->28->28->35->49->49->53->53
Output: 23->35

Input: 11->11->11->11->75->75
Output: empty List

请注意,这与从链接列表中删除重复项不同

这个想法是维护一个指向节点的指针(prev) ,该节点恰好位于我们正在检查重复的节点块之前。在第一个示例中,当我们检查节点 28 的重复项时,指针prev将指向 23。一旦我们到达值为 28 的最后一个重复节点(将其命名为当前指针),我们可以使 prev 节点的下一个字段成为当前的下一个并更新current=current.next 。这将删除具有重复项的值为 28 的节点块。

Python3
# Python3 implementation for the 
# above approach
  
# Creating node
class Node:
    def __init__(self, val):
        self.val = val
        self.next = None
class LinkedList:
    def __init__(self):
        self.head = None
          
    # Add node into beganing of linked list
    def push(self, new_data):
        new_node = Node(new_data)
        new_node.next = self.head
        self.head = new_node
        return new_node
          
    # Function to remove all occurrences
    # of duplicate elements
    def removeAllDuplicates(self, temp):
          
        # temp is head node of linkedlist
        curr = temp
  
        # print(' print something')
        head = prev = Node(None)
        head.next = curr
  
        # Here we use the same as we do in removing 
        # duplicates and the only extra thing is that
        # we need to remove all elements 
        # having duplicates that we did in 30-31
        while curr and curr.next:
              
            # until the current value and next value 
            # are same keep updating the current value
            if curr.val == curr.next.val:
                while(curr and curr.next and 
                      curr.val == curr.next.val):
                    curr = curr.next
                      
                    # still one of duplicate values left
                    # so point prec to curr
                curr = curr.next
                prev.next = curr
            else:
                prev = prev.next
                curr = curr.next
        return head.next
          
    # for print the linkedlist
    def printList(self):
        temp1 = self.head
        while temp1 is not None:
            print(temp1.val, end = " ")
            temp1 = temp1.next
              
    # For getting head of linkedlist
    def get_head(self):
        return self.head
  
# Driver Code
if __name__=='__main__':
    llist = LinkedList()
    llist.push(53)
    llist.push(53)
    llist.push(49)
    llist.push(49)
    llist.push(35)
    llist.push(28)
    llist.push(28)
    llist.push(23)
      
    print('Created linked list is:')
    llist.printList()
    print(
    'Linked list after deletion of duplicates:')
    head1 = llist.get_head()
    #print(head1)
    llist.removeAllDuplicates(head1)
    llist.printList()
          
# This code is contributed by PRAVEEN KUMAR(IIIT KALYANI)


输出:

List before removal of duplicates
23 28 28 35 49 49 53 53 
List after removal of duplicates
23 35 

时间复杂度: O(n)
有关详细信息,请参阅有关从排序的链接列表中删除所有重复项的完整文章!