Remove duplicate nodes unidirectional ordered list is non-empty, is not retained duplicate nodes

Title Description

In a sorted linked list nodes duplicate, delete the duplicate node list, the node does not retain repeated, returns the head pointer list. For example, the list 1-> 2-> 3-> 3-> 4-> 4-> 5 is treated 1-> 2-> 5
 
The algorithm itself on the guest editor of cattle after Baidu search a bit by other people's actions and found that we are used to solve the two-cycle, I still prefer to run again, ran their own ideas is deleted, and retention repeat the practice of nodes the same, but in a repeat deleted after completion of the node, the node itself and then delete, and then continue to compare, but always felt that there is a defect, looking very uncomfortable, look no nested loops the comfortable, being the first posted Come
* DeleteDuplication ListNode (ListNode * PHEAD) 
    { 
        ListNode * P = PHEAD, * PF = NULL;
         int  var ;
         the while (p-&& P> Next) {
             IF (p-> next-> p-Val ==> Val) { 
                ListNode * = p-Q> Next; 
                P -> = Q- Next> Next;
                 Free (Q);
                 var = p-> Val; 
            } the else {
                 IF (p-> Val == var ) { // remove duplicate as node identification of 
                    IF (!pf){ 
                        pHead = pHead->next;
                        free(p);
                        p = pHead;
                    }else{
                        pf->next = p->next;
                        free(p);
                        p = pf->next;
                    }
                }else{
                    pf = p;
                    p = p->next;
                }
            }
        }
// this is handled behind the most tedious, looking at redundant, because the last node may still exist after the loop exits and may be the last duplicate nodes, so here pay more a judgment processing operation, but looked really ah ah very sick
IF (p-&& P> Val == var ) { IF (! {PF) PHEAD = pHead-> Next; Free (P); P = PHEAD; } the else { PF -> Next = P -> Next; Free (P); P = pf-> Next; } } return PHEAD; }

 

Guess you like

Origin www.cnblogs.com/GuoYuying/p/11488676.html