zoukankan      html  css  js  c++  java
  • 奇数在前偶数在后。各自反转后相连

    #include <iostream>
    #include <stdlib.h>
    using namespace std;
    
    struct node {
        struct node *next;
        int value;
    };
    node *CreateListNode(int value)
    {
        if(value==NULL)
            return NULL;
        node *pNode=(node*)malloc(sizeof(node));
        pNode->value=value;
        pNode->next=NULL;
        return pNode;
    }
    void ConnectNodes(node*pCurrent,node* pNext)
    {
        if (pCurrent==NULL)
        {
            cout<<"Error to connect two nodes."<<endl;
            exit(1);
        }
        pCurrent->next=pNext;
    }
    void PrintList(node* pHead)
    {
        node *pNode=pHead;
        while (pNode!=NULL)
        {
            cout<<pNode->value<<" ";
            pNode=pNode->next;
        }
        cout<<endl;
    }
    struct node *reverse(struct node *list)
    {
        if (list==NULL)
            return NULL;
        if(list->next==NULL)
            return list;
        node* tPre=list;
        node* tmp=list->next;
        while (tmp)
        {
            node* tNext=tmp->next;
            tmp->next=tPre;
            tPre=tmp;
            tmp=tNext;
        }
        list->next=NULL;
        list=tPre;
        return list;
    }
    struct node *swap(struct node *list)
    {
        if (list==NULL)
            return NULL;
        if (list->next==NULL)
            return list;
        node *p=list;
        node *OddHead=(node*)malloc(sizeof(node));
        node *EvenHead=(node*)malloc(sizeof(node));
        node *Odd=OddHead;
        node *Even=EvenHead;
        while (p)
        {
            if (p->value%2==0)
            {
                Even->next=p;
                Even=p;
            }
            else
            {
                Odd->next=p;
                Odd=p;
            }
            p=p->next;
        }
        Even->next=NULL;
        Odd->next=NULL;
    
        Even=reverse(EvenHead->next);
        Odd=reverse(OddHead->next);
        OddHead=Odd;
        while (Odd->next)
            Odd=Odd->next;
    
        Odd->next=Even;
        return OddHead;
        //return OddHead;
    }
    
    int main()
    {
        node *pNode1=CreateListNode(4);
        node *pNode2=CreateListNode(5);
        node *pNode3=CreateListNode(7);
        node *pNode4=CreateListNode(1);
        node *pNode5=CreateListNode(6);
    
        ConnectNodes(pNode1,pNode2);
        ConnectNodes(pNode2,pNode3);
        ConnectNodes(pNode3,pNode4);
        ConnectNodes(pNode4,pNode5);
    
        node* p=swap(pNode1);
        PrintList(p);
        return 0;
    }
  • 相关阅读:
    爬虫前奏
    虚拟机初始配置以及NAT模式下的联网配置
    VMware环境下安装CentOS
    牛客 华华对月月的忠诚 【规律】公因数
    CF1324A Yet Another Tetris Problem
    CF1325B CopyCopyCopyCopyCopy 【思维】【map】
    CF1325A EhAb AnD gCd【思维题】
    同济网络赛 排列计算【差分,前缀和】
    同济大学网络赛 张老师和菜哭武的游戏
    CF A. Phoenix and Balance Codeforces Round #638 (Div. 2) 5月1号
  • 原文地址:https://www.cnblogs.com/xiaodi914/p/5853253.html
Copyright © 2011-2022 走看看