比如:1(头)->2->2->3->3->1->1(头) 去除以后的结果是1->2->3,注意头尾的1也要去掉一个。
#include "stdafx.h"#includeusing namespace std;struct tnode{ tnode* next; int value;};tnode* unique(tnode *head){ tnode* p = head; while (p->next != head) { if (p->next->value == p->value) { tnode* temp = p->next; p->next = temp->next; delete temp; } else { p = p->next; } } if (p->value == head->value) { p->next = head->next; delete head; head = p; } return head;};void print(tnode* head){ tnode* i = head; while (i->next != head) { cout << i->value << " "; i = i->next; } cout << i->value << endl;}int _tmain(int argc, _TCHAR* argv[]){ tnode* node1 = new tnode; node1->value = 1; tnode* node2 = new tnode; node2->value = 1; tnode* node3 = new tnode; node3->value = 2; tnode* node4 = new tnode; node4->value = 2; tnode* node5 = new tnode; node5->value = 3; tnode* node6 = new tnode; node6->value = 1; node1->next = node2; node2->next = node3; node3->next = node4; node4->next = node5; node5->next = node6; node6->next = node1; tnode* head = unique(node1); print(head); return 0;}