📌  相关文章
📜  使用递归从排序的链表中删除重复项

📅  最后修改于: 2021-04-29 09:57:13             🧑  作者: Mango

编写一个removeDuplicates()函数,该函数接受一个以非降序排列的列表,并从列表中删除所有重复的节点。该列表仅应遍历一次。

例如,如果链接列表是11-> 11-> 11-> 21-> 43-> 43-> 60,则removeDuplicates()会将列表转换为11-> 21-> 43-> 60。

算法:
从头开始(或从头到尾)递归遍历列表,并在完成递归调用之后,比较下一个节点(返回的节点)和当前节点(头)。如果两个节点的数据相等,则返回下一个(head-> next)节点,否则返回当前节点(head)

执行:
除了removeDuplicates()以外的函数仅用于创建链接的链表并测试removeDuplicates()。

C++
/* C Program to remove duplicates
 from a sorted linked list */
#include 
#include 
  
/* Link list node */
struct Node {
    int data;
    struct Node* next;
};
  
/* The function removes duplicates from a sorted list */
struct Node* removeDuplicates(struct Node* head)
{
    /* if head is null then return*/
    if (head == NULL)
        return NULL;
  
    /* Remove duplicates from list after head */
    head->next = removeDuplicates(head->next);
  
    // Check if head itself is duplicate
    if (head->next != NULL && 
        head->next->data == head->data) {
  
        Node* res = head->next;
        delete head;
        return res;
    }
  
    return head;
}
  
/* UTILITY FUNCTIONS */
/* Function to insert a node at 
   the beginning of the linked list */
void push(struct Node** head_ref, int new_data)
{
    struct Node* new_node = new Node;
    new_node->data = new_data;
    new_node->next = (*head_ref);
    (*head_ref) = new_node;
}
  
/* Function to print nodes in a given linked list */
void printList(struct Node* node)
{
    while (node != NULL) {
        printf("%d ", node->data);
        node = node->next;
    }
}
  
/* Driver program to test above functions*/
int main()
{
    /* Start with the empty list */
    struct Node* head = NULL;
  
    /* Let us create a sorted linked list to test the functions
    Created linked list will be 11->11->11->13->13->20 */
    push(&head, 20);
    push(&head, 13);
    push(&head, 13);
    push(&head, 11);
    push(&head, 11);
    push(&head, 11);
  
    printf("\n Linked list before duplicate removal ");
    printList(head);
  
    /* Remove duplicates from linked list */
    struct Node* h = removeDuplicates(head);
  
    printf("\n Linked list after duplicate removal ");
    printList(h);
  
    return 0;
}


Java
/* Java Program to remove duplicates 
from a sorted linked list */
class GFG 
{
  
/* Link list node */
static class Node 
{ 
    int data; 
    Node next; 
}; 
  
/* The function removes duplicates from a sorted list */
static Node removeDuplicates( Node head) 
{ 
    /* if head is null then return*/
    if (head == null) 
        return null; 
  
    /* Remove duplicates from list after head */
    head.next = removeDuplicates(head.next); 
  
    // Check if head itself is duplicate 
    if (head.next != null && 
        head.next.data == head.data)
    { 
  
        Node res = head.next; 
          
        return res; 
    } 
  
    return head; 
} 
  
/* UTILITY FUNCTIONS */
/* Function to insert a node at 
the beginning of the linked list */
static Node push( Node head_ref, int new_data) 
{ 
    Node new_node = new Node(); 
    new_node.data = new_data; 
    new_node.next = (head_ref); 
    (head_ref) = new_node;
    return head_ref;
} 
  
/* Function to print nodes in a given linked list */
static void printList( Node node) 
{ 
    while (node != null) 
    { 
        System.out.printf("%d ", node.data); 
        node = node.next; 
    } 
} 
  
/* Driver program to test above functions*/
public static void main(String args[]) 
{ 
    /* Start with the empty list */
    Node head = null; 
  
    /* Let us create a sorted linked list to test the functions 
    Created linked list will be 11.11.11.13.13.20 */
    head = push(head, 20); 
    head = push(head, 13); 
    head = push(head, 13); 
    head = push(head, 11); 
    head = push(head, 11); 
    head = push(head, 11); 
  
    System.out.printf("\n Linked list before duplicate removal "); 
    printList(head); 
  
    /* Remove duplicates from linked list */
    Node h = removeDuplicates(head); 
  
    System.out.printf("\n Linked list after duplicate removal "); 
    printList(h); 
}
} 
  
// This code is contributed by Arnab Kundu


Python
# Python Program to remove duplicates 
# from a sorted linked list 
  
# A linked list node
class Node: 
    def __init__(self, new_data): 
        self.data = new_data 
        self.next = None
  
# The function removes duplicates from a sorted list 
def removeDuplicates( head) :
  
    # if head is None then return
    if (head == None) :
        return None
  
    # Remove duplicates from list after head 
    head.next = removeDuplicates(head.next) 
  
    # Check if head itself is duplicate 
    if (head.next != None and
        head.next.data == head.data):
        res = head.next
          
        return res 
      
    return head 
  
# UTILITY FUNCTIONS 
# Function to insert a node at 
#the beginning of the linked list 
def push( head_ref, new_data) :
    new_node = Node(0) 
    new_node.data = new_data 
    new_node.next = (head_ref) 
    (head_ref) = new_node
    return head_ref
  
# Function to print nodes in a given linked list 
def printList(node) :
  
    while (node != None) :
      
        print(node.data,end=" ") 
        node = node.next
      
# Driver program to test above functions
  
# Start with the empty list 
head = None
  
# Let us create a sorted linked list to test the functions 
# Created linked list will be 11.11.11.13.13.20 
head = push(head, 20) 
head = push(head, 13) 
head = push(head, 13) 
head = push(head, 11) 
head = push(head, 11) 
head = push(head, 11) 
  
print("\n Linked list before duplicate removal ") 
printList(head) 
  
# Remove duplicates from linked list 
h = removeDuplicates(head) 
  
print("\n Linked list after duplicate removal ") 
printList(h) 
  
# This code is contributed by Arnab Kundu


C#
/* C# Program to remove duplicates 
from a sorted linked list */
using System;
  
class GFG 
{
      
    /* Link list node */
    public class Node 
    { 
        public int data; 
        public Node next; 
    }; 
      
    /* The function removes duplicates from a sorted list */
    static Node removeDuplicates( Node head) 
    { 
        /* if head is null then return*/
        if (head == null) 
            return null; 
      
        /* Remove duplicates from list after head */
        head.next = removeDuplicates(head.next); 
      
        // Check if head itself is duplicate 
        if (head.next != null && 
            head.next.data == head.data)
        { 
      
            Node res = head.next; 
              
            return res; 
        } 
      
        return head; 
    } 
      
    /* UTILITY FUNCTIONS */
    /* Function to insert a node at 
    the beginning of the linked list */
    static Node push( Node head_ref, int new_data) 
    { 
        Node new_node = new Node(); 
        new_node.data = new_data; 
        new_node.next = (head_ref); 
        (head_ref) = new_node;
        return head_ref;
    } 
      
    /* Function to print nodes in a given linked list */
    static void printList( Node node) 
    { 
        while (node != null) 
        { 
            Console.Write("{0} ", node.data); 
            node = node.next; 
        } 
    } 
      
    /* Driver program to test above functions*/
    public static void Main(String []args) 
    { 
        /* Start with the empty list */
        Node head = null; 
      
        /* Let us create a sorted linked list to test the functions 
        Created linked list will be 11.11.11.13.13.20 */
        head = push(head, 20); 
        head = push(head, 13); 
        head = push(head, 13); 
        head = push(head, 11); 
        head = push(head, 11); 
        head = push(head, 11); 
      
        Console.Write("\n Linked list before duplicate removal "); 
        printList(head); 
      
        /* Remove duplicates from linked list */
        Node h = removeDuplicates(head); 
      
        Console.Write("\n Linked list after duplicate removal "); 
        printList(h); 
    }
}
  
/* This code contributed by PrinciRaj1992 */


输出:
Linked list before duplicate removal 11 11 11 13 13 20 
 Linked list after duplicate removal 11 13 20