The code implements the circular Unidirectional link list (the tail pointer points directly to the head pointer, there is no empty node in the middle), and the operation of successive repeating elements is removed.
For example: 1 (head)->2->2->3->3->1->1 (head) after the removal of the result is 1->2->3, notice that the tail of 1 also to remove one.
The time complexity is O (N)//Space complexity is O (1)//code as follows: #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <climits> #include <cmath> #include <queue> #include <cassert># Include <algorithm> #define MAXN 10010#define RST (n) memset (n, 0, sizeof (n)) using namespace std;typedef int data_t; typedef struct listnode{data_t data; struct ListNode *next;} Lnode, *pnode;pnode Create (int n, int *hashtable) {Pnode head = NULL; Pnode P1 = null, p2 = null; P1 = P2 = (pnode) malloc (sizeof (Lnode)); while (n--) {cin >> p1->data; hashtable[p1->data]++; Statistics if (head = = NULL) head = p1; else P2->next = p1; P2 = p1; P1 = (pnode) malloc (sizeof (Lnode)); } P2->next = head; Point to Header return head;} void Uniquelistnode (pnode head, int *hashtable) {assert (head! = NULL); Pnode pre = head; Pnode cur = head->next; while (cur! = Head &&cur = NULL)//de-weight {pnode p = cur; Pnode Next = cur->next; if (hashtable[cur->data] = = 1) pre = cur; else if (Hashtable[cur->data] > 1) {hashtable[cur->data]--; Pre->next = Next; Free (p); Release} cur = Next; }}int Main () {int n; while (CIN >> n) {int HASHTABLE[MAXN] = {0}; The number of hash storage occurrences Pnode head = Create (n, hashTable); Construct a circular single-linked list Uniquelistnode (head, hashTable); To re-/***** result *****/cout << head->data << "--"; For (Pnode p=head->next; p!=head; p=p->next) {cout << p->data; if (p->next! = head) cout << "-"; else cout << Endl; }} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Google Pen Test---ring list to go heavy