LeetCode:LC67 remove-duplicates-from-sorted-list

Title description

Given a sorted linked list, delete all repeated elements in the linked list, and only keep the elements that appear only once in the original linked list.

E.g:

The linked list given is 1->2->3->3->4->4->5, return 1->2->5.

The linked list given is 1->1->1->2->3, return 2->3.

enter

{1,2,2}

Output

{1}

Procedure flow chart:

First create a linked list according to the input value of the pre-interpolation method, and then return the head pointer of the new linked list after processing in the deleteDuplicates function, and then output the new linked list elements in turn.

The idea is not a general idea, and a bit cumbersome. I can only say that my thinking likes detours, right? Not flexible enough, not as simple as other big guys wrote hahaha!

 

Readers who want to analyze the code can cite some examples into the code, and the analysis step by step will probably be clear. The author will not analyze it, because the thinking is more complicated. The initial linked list of different lengths should be modified in the corresponding length value in main.

Runnable code:

#include<iostream>
#include<stdio.h>
#include<malloc.h>
using namespace std;
//定义结构体
typedef struct ListNode
{
int val;
struct ListNode *next;
}ListNode;

ListNode* deleteDuplicates(ListNode* head){
        // write code here

        //flag用来标记first
           ListNode* first,*last;
           int flag=0;int flag2=0;

        while(1)
        {
        if(head->val!=head->next->val)
        {
            break;
        }
            head=head->next;
        }
        
//head指针移动到新链表尾部时,将first指针赋给head指针
        while(head){
            if(!head->next)
            {
                head=first;
                break;
            }

     
            //1 2 中的1这种情况
            else if(head->val!=head->next->val&&flag==0)
            {
                //first表示新链表的队首指针
                first=head;
                flag=1;
                last=head;
                head=head->next;
            }
            else if(head->val!=head->next->val&&flag==1)
            {
                head=head->next;
                last=last->next;
            }

//head指向的节点值和head指向节点的后一个节点值相等时
            else if(head->val==head->next->val)
            {
                while(1)
                {
                    if(head->next==NULL)
                    {
                        flag2=1;
                        break;
                    }
                    else if(head->val!=head->next->val)
                    {
                        break;
                    }
                head=head->next;
                }
                if(flag2==0)
                {
                head=head->next;
                last->next=head;
                }
                else
                {
                last->next=head->next;
                }
            }

        }
      return first;
}
int main()
{
ListNode *L=new ListNode;
L->next=NULL;
ListNode *r=L;
//4表示链表元素为4个,此处可修改
for(int i=0;i<4;i++)
{
    ListNode *p=new ListNode;
    cin>>p->val;
    r->next=p;
    r=p;
}
ListNode *q=L;
ListNode * first=deleteDuplicates(L);
//依次输出处理后的链表元素
while(first)
{
    first=first->next;
    cout<<first->val<<' ';
}
}

 

Guess you like

Origin blog.csdn.net/Zhongtongyi/article/details/107282266