Skip to content

Instantly share code, notes, and snippets.

@yiyizheliu
Created April 12, 2015 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yiyizheliu/e672737559ffe5a7bf51 to your computer and use it in GitHub Desktop.
Save yiyizheliu/e672737559ffe5a7bf51 to your computer and use it in GitHub Desktop.
/*
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