The code implementation removes duplicates of C and JAVA instances from the unordered list

  • 2020-04-01 02:21:51
  • OfStack

How do you code the implementation if you can't use temporary caching?


 Method 1: operate directly on the original linked list without using additional storage space. Start with a pointer to the header node of the chain, and then iterate over the node behind it to delete the node that has the same data as the node referred to by the pointer. Then move the pointer back a bit and continue. Until the pointer is moved to the list. 
void delete_duplicate1(node* head){
    node*pPos=head->next;
    node*p,*q;
    while(pPos!=NULL){//Use the pPos pointer to indicate where you are currently moving to
        p=pPos;
       q=pPos->next;
       while(q!=NULL){//Walk through all nodes after pPos, find the node whose value is the same as that of the node referred to by pPos, and delete it
            if(pPos->data==q->data){
                node*pDel=q;
                p->next=q->next;
                q=p->next;
                free(pDel);
                }
            else{
                p=q;
                q=q->next;
                }
            }
        pPos=pPos->next;
        }
}

Method 2: if extra space is allowed, the complexity of the algorithm can be reduced by changing the time of space. We can do this using a hash table, and since it's an interview question, we can ignore some of the problems that might come with using a hash for the moment and assume that it's perfect. That is, suppose it can hash any integer into a range, no negative indices, no hash collisions, etc.

void delete_duplicate2(node* head)
{
    node*p=head->next;
    node*q=p->next;
    memset(hash,0,sizeof(hash));
    hash[p->data]=1;//Set it to 1 to indicate that the number has already appeared
    while(q!=NULL){
        if(hash[q->data]!=0){
            node*pDel=q;
            p->next=q->next;
            q=p->next;
            free(pDel);
            }
        else{
            hash[q->data]=1;//Set it to 1 to indicate that the number has already appeared
            p=q;
            q=q->next;
            }
        }
}

JAVA reference code:


public static void deleteDups(LinkedListNode n) {
  Hashtable table = new Hashtable();
  LinkedListNode previous = null;
  while (n != null) {
    if (table.containsKey(n.data)) previous.next = n.next;
    else {
      table.put(n.data, true);
      previous = n;
    }
    n = n.next;
  }
}
public static void deleteDups2(LinkedListNode head) {
    if (head == null) return;
    LinkedListNode previous = head;
    LinkedListNode current = previous.next;
    while (current != null) {
      LinkedListNode runner = head;
      while (runner != current) { // Check for earlier dups
        if (runner.data == current.data) {
          LinkedListNode tmp = current.next; // remove current
          previous.next = tmp; 
          current = tmp; // update current to next node
          break; // all other dups have already been removed
        }
        runner = runner.next;
      }
      if (runner == current) { // current not updated - update now
        previous = current;
        current = current.next;
      }
    }
 }


Related articles: