Encoding implementation removes duplicates from unordered lists (C and Java instances) _java

Source: Internet
Author: User

If temporary caching is not possible, how do you encode the implementation?

Copy Code code as follows:

Method One: Do not use additional storage space, directly on the original list of operations. Start with a pointer to the linked table header node, and then iterate through the node behind it, removing the node that is the same as the node data that the pointer refers to. Then move the pointer one bit back and continue with the above operation. Until the pointer moves to the linked list.

void Delete_duplicate1 (node* head) {
node*ppos=head->next;
Node*p,*q;
while (ppos!=null) {//PPOs pointer is used to indicate where the current move is.
P=ppos;
q=ppos->next;
while (q!=null) {//Traverse all nodes after PPOs, find the node with the same node value as PPOs, 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 Two: If the extra space is allowed, it can reduce the complex system of the algorithm by space Exchange time. You can use the hash table to complete, since it is the interview question, we can temporarily not consider using hash may bring some problems, first of all assume it is perfect. That is, if it can be arbitrary integer hash to a certain range, there will be no negative numbers subscript, there will be no hash conflict.
Copy Code code as follows:

void Delete_duplicate2 (node* head)
{
node*p=head->next;
node*q=p->next;
memset (hash,0,sizeof (hash));
hash[p->data]=1;//is set to 1, indicating that the number has already occurred
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;//is set to 1, indicating that the number has already occurred
p=q;
q=q->next;
}
}
}

Java Reference Code:

Copy Code code as follows:

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 isn't updated-update now
previous = current;
current = Current.next;
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.