当前位置:   article > 正文

重排链表_给定一个单链表 l1→l2→ →ln 1→ln,请编写程序将链表重新排列为 ln→l1→

给定一个单链表 l1→l2→ →ln 1→ln,请编写程序将链表重新排列为 ln→l1→

给定一个单链表 L1→L2→⋯→Ln−1→Ln​ ,请编写程序将链表重新排列为 Ln→L1→Ln−1→L2→⋯。例如:给定L为1→2→3→4→5→6,则输出应该为6→1→5→2→4→3。

输入格式:
每个输入包含1个测试用例。每个测试用例第1行给出第1个结点的地址和结点总个数,即正整数N (≤ 1 0 5 10^5 105)。结点的地址是5位非负整数,NULL地址用−1表示。

接下来有N行,每行格式为:

Address Data Next

其中Address是结点地址;Data是该结点保存的数据,为不超过 1 0 5 10^5 105的正整数;Next是下一结点的地址。题目保证给出的链表上至少有两个结点。

输出格式:
对每个测试用例,顺序输出重排后的结果链表,其上每个结点占一行,格式与输入相同。

输入样例:

00100 6
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218

输出样例:

68237 6 00100
00100 1 99999
99999 5 12309
12309 2 00000
00000 4 33218
33218 3 -1

/************************************************
┆  ┏┓   ┏┓ ┆
┆┏┛┻━━━┛┻┓ ┆
┆┃       ┃ ┆
┆┃   ━   ┃ ┆
┆┃ ┳┛ ┗┳ ┃ ┆
┆┃       ┃ ┆
┆┃   ┻   ┃ ┆
┆┗━┓    ┏━┛ ┆
┆  ┃    ┃  ┆      
┆  ┃    ┗━━━┓ ┆
┆  ┃  AC代马   ┣┓┆
┆  ┃           ┏┛┆
┆  ┗┓┓┏━┳┓┏┛ ┆
┆   ┃┫┫ ┃┫┫ ┆
┆   ┗┻┛ ┗┻┛ ┆
************************************************ */
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
#include <string>
#include <sstream>
#include <cstdio>
#include <cstring>
#include<cctype>
#include <math.h>
#include <cmath>
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define lb long double
#define LINF 0x3f3f3f3f3f3f3f3f
#define ull unsigned long long
#define random(x) (rand()%x)
#define sgn(x) (fabs(x) < eps ? 0 : ((x) < 0 ? -1 : 1))
#define rep(i, a, b) for(int i=a;i<b;i++)
#define req(j, a, b) for(int j=a;j<b;j++)
#define mem(a, b) memset(a, b, sizeof(a))
#define PI 3.141592653589793
#define K 20

using namespace std;

int main()
{
    int a[100000][3],b[2],d[100000],adress,number,link,t,i,c=0;
    for(int i=0;i<2;i++)
    {
        cin>>b[i];
    }
    for(i=0;i<b[1];i++)
    {
        cin>>adress>>number>>link;
        a[adress][0]=adress;
        a[adress][1]=number;
        a[adress][2]=link;
    }
    int temp=b[0];
    for(i=0;a[temp][2]!=-1;i++)//遍历链表,将地址从头到位储存到b数组中
    {
        d[i]=a[temp][0];
        temp=a[temp][2];
        c++;
    }
    d[i]=a[temp][0];
    c++;
    for(int i=0;i<c/2;i++)
    {
        a[d[c-i-1]][2]=d[i];
        a[d[i]][2]=d[c-i-2];
    }
    if(c%2==0)
    {a[d[c/2-1]][2]=-1;}
    else
    {
        a[d[b[1]/2]][2]=-1;
    }
    t=a[d[c-1]][0];
    for(i=0;a[t][2]!=-1;i++)
    {
        cout<<setw(5)<<setfill('0')<<a[t][0]<<" "<<a[t][1]<<" "<<setw(5)<<setfill('0')<<a[t][2]<<endl;
        t=a[t][2];
    }
    cout<<setw(5)<<setfill('0')<<a[t][0]<<" "<<a[t][1]<<" "<<a[t][2]<<endl;
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/226821
推荐阅读
相关标签
  

闽ICP备14008679号