题目:
删除链表中的重复节点。
思路:
可以用bitmap实现,本文中假设数据在0-20中。
代码:
/*
typedef struct list
{
int value ;
struct list *next ;
}*ListPtr , ListNode ;
*/
void rmDulicate( ListPtr list )
{
int array[21] ;
int i = 0 ;
ListPtr tempList ;
ListPtr tempListParent ;
if( list == NULL )
return ;
for( i = 0 ; i < 20 ; i++ )
{
array[i] = 0 ;
}
tempList = list ;
tempListParent = list ;
while( tempList != NULL )
{
if( array[tempList->value] == 1 )
{
tempListParent->next = tempList->next ;
}
else
{
array[tempList->value] = 1 ;
}
tempListParent = tempList ;
tempList = tempList->next ;
}
}
改进:
1.在我的设计中用数组充当了bitmap,其实因为数据比较小,可以分配一个int来充当bitmap,我的开发环境中int为4字节,那么通过移位操作可以有去重32个数。
2.对于数据比较大的情况,假设最大数据很大,我建议用hash来实现。