Google Pen Test---ring list to go heavy

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.