0
点赞
收藏
分享

微信扫一扫

剑指offer 删除链表中重复的结点(C++)(实现函数的模块化设计)


题目描述

在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5

解题思路

  1. 链表头指针判空。
  2. 将链表中的元素复制到vector中。
  3. 清空链表,保留头指针。
  4. 删除vector中所有重复的元素。
  5. 尾插法重新将vector中的元素赋值到链表中。

代码实现

/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
*/
class Solution {
public:
ListNode* deleteDuplication(ListNode* pHead)
{
if(pHead == NULL)
return NULL;
vector<int> ivec;
ivec = CopyFromLinkListToVector(pHead);

FreeLinkList(pHead);

DeleteAllDuplication(ivec);

InsertLinkListRear(pHead, ivec);

return pHead;
}

vector<int> CopyFromLinkListToVector(ListNode* &pHead)
{
vector<int> ivec;
if(pHead == NULL)
return {};
ListNode *p = pHead;
for(; p; p = p->next)
ivec.push_back(p->val);
return ivec;
}

void FreeLinkList(ListNode* &pHead)
{
free(pHead);
pHead = NULL;
return;
}

void DeleteAllDuplication(vector<int> &ivec)
{
if(ivec.empty())
return;
vector<int>::iterator iter = ivec.begin();
int flag = 0;
while (iter != ivec.end())
{ // 1 2 3 3 4 4 5
flag = count(iter, ivec.end(), *iter);
if (flag > 1)
while (flag--)
iter = ivec.erase(iter);
else
iter++;
}
return;
}

void InsertLinkListRear(ListNode* &pHead, vector<int> &ivec)
{
if(ivec.empty())
return;
ListNode* p = NULL, *temp = pHead;
vector<int>::iterator iter = ivec.begin();
while(iter != ivec.end())
{
p = new ListNode(*iter++);
if(pHead)
{
temp->next = p;
temp = p;
}
else
{
pHead = p;
temp = p;
}

}
}
};

VS2013上的实现:

#include<iostream>
#include<vector>
#include<algorithm>
// 1 2 3 3 4 4 5

struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};

ListNode* CreateLinkList()
{
ListNode* pHead = new ListNode(1);
ListNode* p1 = new ListNode(2);
pHead->next = p1;
ListNode* p2 = new ListNode(3);
p1->next = p2;
ListNode* p3 = new ListNode(3);
p2->next = p3;
ListNode* p4 = new ListNode(4);
p3->next = p4;
ListNode* p5 = new ListNode(5);
p4->next = p5;
ListNode* p6 = new ListNode(5);
p5->next = p6;
return pHead;
}

vector<int> CopyFromLinkListToVector(ListNode* &pHead)
{
vector<int> ivec;
if (pHead == NULL)
return{};
ListNode *p = pHead;
for (; p; p = p->next)
ivec.push_back(p->val);
return ivec;
}

void FreeLinkList(ListNode* &pHead)
{
free(pHead);
pHead = NULL;
return;
}

void DeleteAllDuplication(vector<int> &ivec)
{
if (ivec.empty())
return;
vector<int>::iterator iter = ivec.begin();
int flag = 0;
while (iter != ivec.end())
{ // 1 2 3 3 4 4 5
flag = count(iter, ivec.end(), *iter);
if (flag > 1)
while (flag--)
iter = ivec.erase(iter);
else
iter++;
}
return;
}

void InsertLinkListRear(ListNode* &pHead, vector<int> &ivec)
{
if (ivec.empty())
return;
ListNode* p = NULL, *temp = pHead;
vector<int>::iterator iter = ivec.begin();
while (iter != ivec.end())
{
p = new ListNode(*iter++);
if (pHead)
{
temp->next = p;
temp = p;
}
else
{
pHead = p;
temp = p;
}
}
}

ListNode* deleteDuplication(ListNode* pHead)
{
if (pHead == NULL)
return NULL;
vector<int> ivec;
ivec = CopyFromLinkListToVector(pHead);

FreeLinkList(pHead);

DeleteAllDuplication(ivec);

InsertLinkListRear(pHead, ivec);

return pHead;
}

void PrintLinkList(ListNode* pHead)
{
if (pHead == NULL)
return;
ListNode* p = pHead;
for (; p; p = p->next)
{
cout << p->val << " ";
}
cout << endl;
}

void main()
{
ListNode* pHead = CreateLinkList();
PrintLinkList(pHead);
deleteDuplication(pHead);
cout << "整理之后:" << endl;
PrintLinkList(pHead);
system("pause");
return;
}

运行截图:
1 2 3 3 4 5 5
整理之后:
2 4
请按任意键继续. . .


举报

相关推荐

0 条评论