删除单链表中重复的元素
2010-07-10 17:44
302 查看
/article/4670721.html
用Hashtable辅助,遍历一遍单链表就能搞定。
实践中发现,curr从表头开始,每次判断下一个元素curr.Next是否重复,如果重复直接使用curr.Next = curr.Next.Next; 就可以删除重复元素。
#include <afxtempl.h>// CMap
// remove duplicated elements from the list
node* RemoveDup(node* head)
{
CMap<int, int, node*, node*> ht;// a Hashtable to store values
if ( head != NULL )
{
node* p = head;
ht[p->data] = p;// put the first value into the table
while( p->next )
{
if ( ht[p->next->data] )// p->next has a dup value
{
node* tmp = p->next;
p->next = p->next->next;
delete tmp;// remove the dup, but p does not move forward
}
else
{
ht[p->next->data] = p->next;// store the p->next value
p = p->next;// p moves forward
}
}
}
return head;
}
用Hashtable辅助,遍历一遍单链表就能搞定。
实践中发现,curr从表头开始,每次判断下一个元素curr.Next是否重复,如果重复直接使用curr.Next = curr.Next.Next; 就可以删除重复元素。
#include <afxtempl.h>// CMap
// remove duplicated elements from the list
node* RemoveDup(node* head)
{
CMap<int, int, node*, node*> ht;// a Hashtable to store values
if ( head != NULL )
{
node* p = head;
ht[p->data] = p;// put the first value into the table
while( p->next )
{
if ( ht[p->next->data] )// p->next has a dup value
{
node* tmp = p->next;
p->next = p->next->next;
delete tmp;// remove the dup, but p does not move forward
}
else
{
ht[p->next->data] = p->next;// store the p->next value
p = p->next;// p moves forward
}
}
}
return head;
}
相关文章推荐
- 删除单链表中重复的元素
- 数据结构——链表之单链表重复元素的删除
- 删除单链表中重复的元素
- 删除单链表中的重复元素
- 数据结构之 线性表---单链表的操作B(先逆序+再删除重复元素)
- 利用单链表删除重复元素
- 删除单链表中的重复元素
- 单链表中重复元素的删除
- 13.删除单链表中重复的元素
- 非递减单链表删除重复元素
- 数据结构之 线性表---单链表的应用(重复元素删除)
- 删除单链表中重复的元素
- LeetCode.83. Remove Duplicates from Sorted List(删除单链表中的重复元素)
- 删除数组中重复元素
- 数据结构顺序表删除重复元素
- 删除ArrayList中重复的元素
- 删除单链表中所有值为x的元素
- 删除数组中重复出现的元素,只留下一个,排序后输出
- STL双向链表之删除重复元素(list)
- 删除PHP数组中的重复元素的实现代码