题目描述
在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5
思路:注意空链表的情况
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
*/
class Solution {
vector<int> vc;
bool ifRep(int x)
{
int len=vc.size();
int temp=0;
for(int i=0;i<len;i++)
{
if(vc[i]==x)
{
temp++;
if(temp>1) return true;
}
}
return false;
}
public:
ListNode* deleteDuplication(ListNode* pHead)
{
if(pHead==NULL)
{
return pHead;
}
ListNode* root=pHead;
while(root!=NULL)
{
vc.push_back(root->val);
root=root->next;
}
ListNode* node_l=pHead;
ListNode* node_n=pHead->next;
while(node_n!=NULL)
{
if(ifRep(node_n->val))
{
node_l->next=node_n->next;
node_n= node_l->next;
}
else
{
node_l=node_n;
node_n=node_n->next;
}
}
if(ifRep(pHead->val))
{
return pHead->next;
}
else
{
return pHead;
}
}
};