删除单链表中重复的元素
2012-10-09 22:29
323 查看
用Hashtable辅助,遍历一遍单链表就能搞定。
/article/4670721.html
实践中发现,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;
}
/article/4670721.html
实践中发现,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;
}
相关文章推荐
- 删除单链表中的重复元素
- 数据结构之 线性表---单链表的应用(重复元素删除)
- 删除单链表中重复的元素
- 利用单链表删除重复元素
- 数据结构——链表之单链表重复元素的删除
- 非递减单链表删除重复元素
- 13.删除单链表中重复的元素
- 删除单链表中重复的元素
- 删除单链表中重复的元素
- 单链表中重复元素的删除
- 删除单链表中的重复元素
- LeetCode.83. Remove Duplicates from Sorted List(删除单链表中的重复元素)
- 数据结构之 线性表---单链表的操作B(先逆序+再删除重复元素)
- java 删除linkedlist链表中重复元素
- 删除排序数组中的重复元素(三种方法的比较)
- SDUT-2122 数据结构实验之链表七:单链表中重复元素的删除
- 面试题精选(84):使序列有序的最少交换次数(minimum swaps) + 删除序列中所有重复的元素
- SDUT_2122 数据结构实验之链表七:单链表中重复元素的删除
- SDUT-数据结构实验之链表七:单链表中重复元素的删除
- leetcode 删除单链表中的倒数第k个元素