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

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

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

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

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 的节点块。

Java
// Java program to remove all occurrences of
// duplicates from a sorted linked list 
  
// class to create Linked lIst 
class LinkedList{
      
// Head of linked list 
Node head = null; 
class Node
{
    // Value in the node 
    int val; 
    Node next;
    Node(int v)
    {        
        // Default value of the next
        // pointer field 
        val = v;
        next = null;
    }
}
  
// Function to insert data nodes into
// the Linked List at the front
public void insert(int data)
{
    Node new_node = new Node(data);
    new_node.next = head;
    head = new_node;
}
  
// Function to remove all occurrences
// of duplicate elements 
public void removeAllDuplicates()
{
    // Create a dummy node that acts like 
    // a fake head of list pointing to the 
    // original head
    Node dummy = new Node(0);
  
    // Dummy node points to the original head
    dummy.next = head;
    Node prev = dummy;
    Node current = head;
  
    while (current != null)
    {
        // Until the current and previous values
        // are same, keep updating current 
        while (current.next != null &&
               prev.next.val == current.next.val)
               current = current.next;
  
        // If current has unique value i.e current
        // is not updated, Move the prev pointer
        // to next node
        if (prev.next == current)
            prev = prev.next;
  
        // When current is updated to the last
        // duplicate value of that segment, make
        // prev the next of current
        else
            prev.next = current.next;
  
        current = current.next;
    }
  
    // Update original head to the next of 
    // dummy node 
    head = dummy.next;
}
  
// Function to print the list elements
public void printList()
{
    Node trav = head;
    if (head == null)
        System.out.print(" List is empty" );
          
    while (trav != null)
    {
        System.out.print(trav.val + " ");
        trav = trav.next;
    }
}
  
// Driver code
public static void main(String[] args)
{
    LinkedList ll = new LinkedList();
    ll.insert(53);
    ll.insert(53);
    ll.insert(49);
    ll.insert(49);
    ll.insert(35);
    ll.insert(28);
    ll.insert(28);
    ll.insert(23);
      
    System.out.println(
    "Before removal of duplicates");
    ll.printList();
  
    ll.removeAllDuplicates();
  
    System.out.println(
    "After removal of duplicates");
    ll.printList();
}
}


输出:

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

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