[Huawei Machine Test exercises]24. Delete duplicate nodes in a linked list, reverse output of remaining nodes

Source: Internet
Author: User

Topic

描述:     题目描述:输入一个不带头节点的单向链表(链表的节点数小于100),删除链表中内容重复的节点(重复的节点全部删除),剩余的节点逆序倒排。要求实现函数: void vChanProcess(strNode * pstrIn,strNode * pstrOut);【输入】 pstrIn:输入一个不带头节点的单向链表【输出】 pstrOut:删除内容重复的节点(重复的节点全部删除),剩余节点逆序输出(不带头节点,链表第一个节点的内存已经申请)。【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出示例 输入链表的内容依次为 6,7,8,8,9,10,6则输出链表的内容依次应该是 10,9,7练习阶段:    

Code

/* ---------------------------------------* Date: 2015-06-31* sjf0115* title: Delete duplicate nodes in the list, reverse output from the remaining nodes * Source: Huawei Machine Test Exercises--------- --------------------------------*/#include <iostream>#include <map>#include <vector>#include "oj.h"using namespace STD;/ * Function: Enter a one-way list without the lead node (the number of nodes in the linked list is less than 100), delete the nodes in the list (duplicate nodes are all deleted), and the remaining nodes are inverted in reverse order. Input: Pstrin: Enter a one-way linked list output without the lead node: pstrout: After deleting the duplicate node, the list of linked lists (not the head node, the first node of the list) has been applied in reverse order. Return: Example: the content of the input list is 6,7,8,8,9,10,6, then the contents of the output list should be 10,9,7*// * This code is still a bug when the input list is 88888888, pstrout use the reference best * /intIchanprocess (Strnode * Pstrin,strnode * pstrout) {if(Pstrin = = NULL | | pstrout = = NULL) {return-1; }//if     map<int,int>Map; strnode* p = pstrin;//Statistics repetition out times     while(p) {if(Map.count (p->data) = =0) {Map.insert ( map<int,int>:: Value_type (P->data,1)); }//if        Else{Map[p->data] = map[p->data]+1; }//elsep = p->pstrnext; }//while    //Output to Pstrout for recurring reverse orderp = Pstrin; vector<int>Vec while(p) {if(Map[p->data] = =1) {Vec.push_back (p->data); }//ifp = p->pstrnext; }//while    intSize = Vec.size ();if(Size = =0){return 0; }//if     for(inti =0; I < size-1; ++i) {strnode* node =NewStrnode ();        Node->data = Vec[i];        Node->pstrnext = pstrout->pstrnext;    Pstrout->pstrnext = node; }//forPstrout->data = vec[size-1];/*printf ("\ n");    strnode* P1 = pstrout;        while (p1) {printf ("*%d*\n", p1->data);    P1 = p1->pstrnext; }//while*/    return 0;}/ * Release linked list * /voidVfreechan (Strnode * pstrchan) {strnode* p = Pstrchan; while(p)        {strnode* node = p; p = p->pstrnext; Free(node); }//while    return;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

[Huawei Machine Test exercises]24. Delete duplicate nodes in a linked list, reverse output of remaining nodes

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.