#include <memory>
#include <iostream>
#include <chrono>
#include <thread>
using namespace std;
struct ListNode{
int val;
shared_ptr<ListNode> next;
};
bool InsertNode(shared_ptr<ListNode>& insertPos,int val)
{
shared_ptr<ListNode> node(new ListNode);
if (!node)
return false;
node->val = val;
node->next = NULL;
insertPos->next = node;
insertPos = node;
return true;
}
bool InitLinkList(shared_ptr<ListNode>& head)
{
if ( (!head) || NULL != head->next )
return false;
head->val = 1;
shared_ptr<ListNode> tail = head;
InsertNode(tail, 1);
InsertNode(tail, 2);
InsertNode(tail, 3);
InsertNode(tail, 3);
return true;
}
void CoutLinkList(const shared_ptr<ListNode>& head)
{
for (shared_ptr<ListNode> node = head;
node; node = node->next)
{
cout << node->val << " ";
}
cout << endl;
}
shared_ptr<ListNode> removeDuplicates(shared_ptr<ListNode>& head)
{
if (head == NULL)
{
return NULL;
}
shared_ptr<ListNode> node = head;
while(node->next != NULL){
if(node->val == node->next->val){
shared_ptr<ListNode> tmp = node->next;
node->next = node->next->next;
}else
{
node = node->next;
}
}
return head;
}
int main()
{
shared_ptr<ListNode> head(new ListNode);
InitLinkList(head);
CoutLinkList(head);
head = removeDuplicates(head);
CoutLinkList(head);
return 0;
}
原文:http://www.cnblogs.com/itdef/p/6103187.html