剑指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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值