-
-
Save yiyizheliu/e672737559ffe5a7bf51 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
When a temperary buffer is not allowed, the function directly operates on the list. | |
From the beginning, the element is compared with the following ones every time. When there is the duplicate one, delete it. | |
Time complexity O(n^2) | |
Space compexity O(1) | |
*/ | |
#include <stdio.h> | |
#include <vector> | |
#include <list> | |
#include <iostream> | |
using namespace std; | |
list deleteDupl(list list1) | |
{ | |
list<int>::iterator l1,l2; | |
int flag=0; | |
for(l1=list1.begin();l1!=list1.end();l1++) | |
{ | |
for(l2=l1;l2!=list1.end();l2++) | |
{ | |
l2++; | |
if(l2==list1.end()) | |
break; | |
if((*l1)==(*l2)) | |
{ | |
flag=1; | |
break; | |
} | |
} | |
if(flag==1) | |
{ | |
list1.erase(l2); | |
flag=0; | |
} | |
} | |
return list1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment