题目说明:
题目解读:
去除队列中的重复元素
示例代码:
#include <iostream>
#include <windows.h>
using namespace std;
class Node
{
public:
int data;
Node* next;
Node( int d )
{
data = d;
next = NULL;
}
};
class Solution
{
public:
Node* insert( Node* head, int data )
{
Node* p = new Node( data );
if( head == NULL )
{
head = p;
}
else if( head->next == NULL )
{
head->next = p;
}
else
{
Node* start = head;
while( start->next != NULL )
{
start = start->next;
}
start->next = p;
}
return head;
}
void display( Node* head )
{
Node* start = head;
while( start )
{
cout << start->data << " ";
start = start->next;
}
}
Node* removeDuplicates( Node* head )
{
if( head == NULL )
{
return NULL;
}
Node* tmp = head;
while( tmp->next != NULL )
{
if( tmp->data == tmp->next->data )
{
tmp->next = tmp->next->next;
}
else
{
tmp = tmp->next;
}
}
return head;
}
};
//by zhaocl
int main()
{
Node* head = NULL;
Solution mylist;
int T, data;
cin >> T;
while( T-- > 0 )
{
cin >> data;
head = mylist.insert( head, data );
}
head = mylist.removeDuplicates( head );
mylist.display( head );
system( "pause" );
return 0;
}
知识点:
链表的插入与移除(指针操作)