附加3 Merging Linked Lists (25 分)

Given two singly linked lists L 1=a 1 →a 2​ →⋯→a n−1​ →a n and L 2 =b 1 →b 2 →⋯→b m−1 →b m . If n≥2m, you are supposed to reverse and merge the shorter one into the longer one to obtain a list like a 1 →a 2 →b m→a 3​ →a 4 →b m−1 ⋯. For example, given one list being 6→7 and the other one 1→2→3→4→5, you must output 1→2→7→3→4→6→5.

Input Specification:
Each input file contains one test case. For each case, the first line contains the two addresses of the first nodes of L 1and L 2​, plus a positive N (≤10 5 ) which is the total number of nodes given. The address of a node is a 5-digit nonnegative integer, and NULL is represented by -1.
Then N lines follow, each describes a node in the format:

Address Data Next

where Address is the position of the node, Data is a positive integer no more than 10^5
, and Next is the position of the next node. It is guaranteed that no list is empty, and the longer list is at least twice as long as the shorter one.

Output Specification:
For each case, output in order the resulting linked list. Each node occupies a line, and is printed in the same format as in the input.

Sample Input:

00100 01000 7
02233 2 34891
00100 6 00001
34891 3 10086
01000 1 02233
00033 5 -1
10086 4 00033
00001 7 -1
结尾无空行

Sample Output:

01000 1 02233
02233 2 00001
00001 7 34891
34891 3 10086
10086 4 00100
00100 6 00033
00033 5 -1
结尾无空行

思路:

概括来说程序要实现两种功能,一是序列逆转,二是序列的合并;
由题意可知,输入数据确保了必有长序列>=2*短序列,所以再保存输入数据后第一件事,便是找出长短序列, 其次便是对短序列进行逆转操作,再之后就是在长序列中每隔两个元素插入短序列的一个元素
主要难点在于在插入操作时,对于节点地址的变化;以及当合并序列中,最后一个节点来自于短序列时,此节点的next_add要为-1,所以要增加一个判断条件,即此时的长序列的节点已经移动到了最后一位 if(i == l1-1)

代码:

#include <stdio.h>
#include <string.h>
#define Maxsize 100005
struct node{
    int data;
    int next_add;
}list[Maxsize];

int cnt_1[Maxsize];
int cnt_2[Maxsize];
int cnt[Maxsize];

void Merge(struct node list[Maxsize], int l1, int l2, int cnt_1[Maxsize], int cnt_2[Maxsize]){
    //对l2进行逆转
    for(int i= 0; i<l2/2; i++){
        int tmp = cnt_2[i];
        cnt_2[i] = cnt_2[l2-i-1];
        cnt_2[l2-i-1] = tmp;
    }
    int j=0; //计数l2
    for(int i=0; i<l1; i++){
        if(i % 2 == 1){
            list[cnt_1[i]].next_add = cnt_2[j];
            if(i == l1-1) list[cnt_2[j]].next_add = -1;
            else list[cnt_2[j]].next_add = cnt_1[i+1];
            j++;
        }
        if(j == l2) break;
    }
}

void PrintN(struct node list[Maxsize], int first_add){
    int sum = 0;
    int now_add =first_add;
    while (now_add != -1){
        cnt[sum++] = now_add;
        now_add = list[now_add].next_add;
    }
    for(int i=0; i<sum; i++){
        if(i == sum-1) printf("%05d %d -1\n", cnt[i], list[cnt[i]].data);
        else printf("%05d %d %05d\n", cnt[i], list[cnt[i]].data, cnt[i+1]);
    }
}

int main(){
    int first_add1, first_add2, n;
    scanf("%d %d %d", &first_add1, &first_add2, &n);

    int now_add;
    for(int i=0; i<n; i++){
        scanf("%d", &now_add);
        scanf("%d %d", &list[now_add].data, &list[now_add].next_add);
    }


    int l1 = 0;
    now_add = first_add1;
    while (now_add != -1){
        cnt_1[l1++] = now_add;
        now_add = list[now_add].next_add;
    }
    int l2 = 0;
    now_add = first_add2;
    while (now_add != -1){
        cnt_2[l2++] = now_add;
        now_add = list[now_add].next_add;
    }

    if(l1 > l2){
        Merge(list, l1, l2, cnt_1, cnt_2);
        PrintN(list, first_add1);
    }
    else {
        Merge(list, l2, l1, cnt_2, cnt_1);
        PrintN(list, first_add2);
    }

    return 0;

}

如果有测试点没过,推荐一个有更多测试数据的链接:2019年9月8日秋季PAT甲级题解-2-1161-Merging Linked Lists (25 分) 双解法

  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值