C++ - Single Linked List - Ideas

I want to write a method to remove consecutive items with duplicate data values from a singly linked list. The method should return the number of items removed. The method should clean up memory as required, and should assume that memory was allocated using new.

For example, passing in the list
->a->b->c->c->a->b->b->b->a->null
should result in
->a->b->c->a->b->a->null
and return 3

The list item definition and function declaration are given below

1
2
3
4
5
6
struct litem {
     char data;
     litem* next;
};

int remove_consecutive_duplicates( litem*& list );


I have a simple logic to check the next element recursively & removing the element if its duplicate.
But, i would like to know how many efficient ways to do this ? All ideas welcome from C++ gurus..
I think the efficient and simple way is scanning the list in linear time ( keeping track of the previously visited node since it's singly linked )
Yes i agree with Bazzy , comparing the consecutive element and deleting them , when they are duplicate is the most efficient method you can use STL instead of structure that will be faster.
you can use STL instead of structure that will be faster

What makes you say that? Remember you can still change the default of struct.
Topic archived. No new replies allowed.