OJ. Delete duplicate elements in the sorted list II

Given a sorted linked list, delete all nodes with repeated numbers, and only keep the numbers that are not repeated in the original linked list.
Insert picture description here

Ideas

Define a fast node and a cur node. The two nodes start from the head at the same time. When the val is the same, cur does not move, and fast goes backward until it is different from the val of cur. Define a prev precursor node to store the previous node of cur; Then delete all nodes between prev and fast, and then continue to iterate;

Attach the code:

struct ListNode* deleteDuplicates(struct ListNode* head)
{
    
    
    //如果链表为空或只有一个结点,直接返回头结点
    if(head==NULL)
    return head;
    if(head->next==NULL)
    return head;
    
    struct ListNode* fast=head->next;
    struct ListNode* cur=head;
    struct ListNode* slow=NULL;

    while(fast)
    {
    
    
        //如果fast与cur的val不相等,三个节点同时向后走
        if(fast->val!=cur->val)
        {
    
    
            slow=cur;
            cur=fast;
            fast=fast->next;
        }
        //fast与cur的val相等
        else
        {
    
    
            //如果fast与cur的val相等,fast往后走,直至与cur的val不相等;
            while(fast&&cur->val==fast->val)
            {
    
    
                fast=fast->next;
            }
            //如果头两个节点的val就相等,删除fast之前的节点,将fast置为新的头;
            if(slow==NULL)
            {
    
    
                head=fast;
            }
            //删除slow与fast中间的节点
            else
            {
    
    
                slow->next=fast;
            }
            cur=fast;
            //如果未到NULL,继续迭代
            if(cur)
                fast=fast->next;
        }
    }
    return head;
}

Guess you like

Origin blog.csdn.net/qq_43745617/article/details/113076862