Remove list of offer prove safety duplicate node

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

Thinking

  1. Defines a vector to hold the node will not be repeated, starting from the initial node, current and next define two pointers, it is determined whether the two nodes is equal to val, if equal, the next move, until it finds a current is not equal to node values ​​should be noted that determining whether the current and next is NULL, the last disadvantage of this method is a skip node, and therefore need to cycle is completed a final determination whether the value of a node is repeated, by last_value to judge. Finally, the nodes in the vector to string together
  2. Recursive, in fact, similar ideas

Code

method one

class Solution {
public:
    ListNode* deleteDuplication(ListNode* pHead)
    {
        if(pHead == NULL)
            return NULL;
        vector<ListNode*> L_node;
        ListNode* current = pHead;
        int last_value;
        while(current != NULL && current->next != NULL)
        {
            ListNode* next = current->next;
            if(current->val!=next->val)
            {
                L_node.push_back(current);
                last_value = current->val;
                current = next;
            }
            else
            {
                while(current->val==next->val)
                {
                    if(next->next == NULL)
                    {
                        next = NULL;
                        break;
                    }
                    else
                    {
                        next = next->next;
                    }
                }
                current = next;
            }
        }
        if(current != NULL && current->val != last_value)
            L_node.push_back(current);
        if(L_node.size() == 0)
            return NULL;
        else
        {
            for(int i = 0; i < L_node.size()-1;i++)
            {
                L_node[i]->next = L_node[i+1];
            }
            L_node[L_node.size()-1]->next = NULL;
            return L_node[0];
        }
    }
};

Method Two:

class Solution {
public:
    ListNode* deleteDuplication(ListNode* pHead)
    {
        if (pHead==NULL)
            return NULL;
        if (pHead!=NULL && pHead->next==NULL)
            return pHead;
                 
        ListNode* current;
         
        if ( pHead->next->val==pHead->val){
            current=pHead->next->next;
            while (current != NULL && current->val==pHead->val)
                current=current->next;
            return deleteDuplication(current);                     
        }
         
        else {
            current=pHead->next;
            pHead->next=deleteDuplication(current);
            return pHead;
        }    
    }
};
Published 85 original articles · won praise 0 · Views 401

Guess you like

Origin blog.csdn.net/weixin_38312163/article/details/104810498