patterncppMinor
Remove duplicates from unsorted linked list
Viewed 0 times
unsortedremovelistfromlinkedduplicates
Problem
Remove duplicate elements from linked list.
This takes \$O(N^2)\$ time. Can we do the same in \$O(N)\$ without the use of hash table? Or if mandatory how can we implement it in C++?
How is the overall code quality of
#include
using namespace std;
class LinkedList{
private:
int data;
LinkedList *next;
public:
void insert(LinkedList **start, int data){
LinkedList *p = new LinkedList;
if (*start == NULL){
p->data = data;
p->next = NULL;
*start = p;
}
else{
LinkedList *temp = *start;
while (temp->next != NULL){
temp = temp->next;
}
temp->next = p;
p->data = data;
p->next = NULL;
}
}
void removeDuplicates(LinkedList **start){//This function removes the duplicates using the standard runner method taking 2 pointers
LinkedList *temp = *start;
LinkedList *temp1 = (*start);
while (temp != NULL){
while (temp1->next!=NULL){
if (temp->data == temp1->next->data){
LinkedList *p;
p = temp1->next;
temp1->next = temp1->next->next;
delete(p);
}
else{
temp1 = temp1->next;
}
}
temp = temp->next;
temp1 = temp;
}
}
void traverse(LinkedList **start){
LinkedList *temp = *start;
while (temp != NULL){
cout data;
temp = temp->next;
}
}
};
int main(){
LinkedList *start = NULL;
LinkedList p1;
p1.insert(&start, 9);
p1.insert(&start, 8);
p1.insert(&start, 7);
p1.insert(&start, 9);
p1.insert(&start, 8);
p1.traverse(&start);
p1.removeDuplicates(&start);
cout << "\n";
p1.traverse(&start);
getchar();
return 0;
}This takes \$O(N^2)\$ time. Can we do the same in \$O(N)\$ without the use of hash table? Or if mandatory how can we implement it in C++?
How is the overall code quality of
Solution
I see a number of things that may help you improve your code.
Make sure you have all required
The code uses
Avoid raw pointers
In modern C++, we tend not to use raw pointers very often. In this case, It would probably be better to have two different classes, one would be a
Use
Modern C++ uses
Match
If you allocate memory using
Don't abuse
Putting
Use more descriptive names
The code's
Omit
If your program completes successfully, the
Make sure you have all required
#includesThe code uses
getchar but doesn't #include . Also, carefully consider which #includes are part of the interface (and belong in the .h file) and which are part of the implementation.Avoid raw pointers
In modern C++, we tend not to use raw pointers very often. In this case, It would probably be better to have two different classes, one would be a
LinkedList class and the other would be a Node class. That way, instead of starting with a pointer, you can start with an object.Use
nullptr rather than NULLModern C++ uses
nullptr rather than NULL. See this answer for why and how it's useful. Match
new with deleteIf you allocate memory using
new, you must also free it using delete or your program will leak memory. Since you use new in insert(), you should use delete in the LinkedList destructor which you have not yet written.Don't abuse
using namespace stdPutting
using namespace std within your program is generally a bad habit that you'd do well to avoid. Use more descriptive names
The code's
traverse function actually prints the nodes. For that reason it should probably be named something like print(). Even better would be to have such a function take a std::ostream& argument so it would be possible to print to something other than std::cout.Omit
return 0If your program completes successfully, the
return 0 at the end of main() will be generated automatically, so it's not needed in C++ programs.Context
StackExchange Code Review Q#115218, answer score: 6
Revisions (0)
No revisions yet.