2019年9月8日秋季PAT甲级题解-2-1161-Merging Linked Lists (25 分)

38 篇文章 0 订阅

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​1​​ and 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

链表等于不难
将l1设为长得,l2设为短的。然后就是输出比较难了,通过add作为下标的就是静态链表,输出只要找对下一个结点是啥,输出它的地址就行,不用改自己的next
分三种情况讨论:

//每隔两个长的就输出一个短的,记得最后一位要单独处理 
	//最后一位如何处理?
	//3种情况,1.是短的,判断长的最后一位已经输出了,短的是自己的最后一位,就输出-1
	//2.是第一位的长的,判断此时短的是否输出完了,且长的是否是自己的最后一位,就输出-1
	//3.是第二位的长的,判断此时短的是否输出完了,且长的是否是自己的最后一位,就输出-1 
//每隔两个插入一个
//静态链表,搞多个vector分别放即可
//不需要修改next,只需要直接输出下一位该输出的add即可 
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
struct node{
	int add;
	int val;
	int next;
}nodes[100000];
int main(){
	int s1,s2,n;
	cin>>s1>>s2>>n;
	int add,val,next;
	vector<node> l1;
	vector<node> l2;
	for(int i = 0; i < n; i++){
		cin>>add>>val>>next;
		nodes[add] = {add,val,next};
	}
	for(int i = s1; i != -1; i = nodes[i].next){
		l1.push_back(nodes[i]);
	}
	for(int i = s2; i!=-1; i = nodes[i].next){
		l2.push_back(nodes[i]);
	}
	vector<node> temp;
	if(l1.size() > l2.size()){
		reverse(l2.begin(),l2.end());
	}
	else if(l1.size() < l2.size()){
		temp = l1;
		l1 = l2;
		l2 = temp;
		reverse(l2.begin(),l2.end());
	}
	//每隔两个长的就输出一个短的,记得最后一位要单独处理 
	//最后一位如何处理?
	//3种情况,1.是短的,判断长的最后一位已经输出了,短的是自己的最后一位,就输出-1
	//2.是第一位的长的,判断此时短的是否输出完了,且长的是否是自己的最后一位,就输出-1
	//3.是第二位的长的,判断此时短的是否输出完了,且长的是否是自己的最后一位,就输出-1 
	int index = 0;
	for(int i = 0; i < l1.size(); i++){
		if(i%2 == 0){
			if(index == l2.size() && i == l1.size()-1) printf("%05d %d -1",l1[i].add,l1[i].val);
			else printf("%05d %d %05d\n",l1[i].add,l1[i].val,l1[i+1].add);
		}
		else{
			if(index == l2.size() && i == l1.size()-1){
				printf("%05d %d -1",l1[i].add,l1[i].val);//长的作为最后一位 
				break;
			}
			else printf("%05d %d %05d\n",l1[i].add,l1[i].val,l2[index].add);
			if(i == l1.size() -1 && index == l2.size() -1) printf("%05d %d -1",l2[index].add,l2[index].val);
			else printf("%05d %d %05d\n",l2[index].add,l2[index].val,l1[i+1].add);
			index++; 
		}
	}
	return 0;
} 

二刷
将l1设为长的,l2设为短的并反转。每两个输出一下l2,分情况处理,看最后一个是l1里的还是l2里的。
法2.:每两个l1的结点加入,就一个l2的加入,要判断l2的结点数有没有越界,最后一个vector轻松输出

//将l1设为长的那条,对L2进行反转,然后输出l1是同时,每两个输出一下l2
//最后一个要单独处理,因为l1是l2的两倍长
//所以有两种情况,一种是l2做结尾:l1输出完了,l2也是最后一个;一种是l1做结尾,l2输出完了,l1也是最后一个
//或者直接将其放入一个链表 
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
using namespace std;
struct node{
	int add;
	int val;
	int next;
}nodes[100005];
vector<node> l1,l2,ans;
int main(){
	int s1,s2,n;
	cin>>s1>>s2>>n;
	int add,val,next;
	for(int i = 0; i < n; i++){
		cin>>add>>val>>next;
		nodes[add] = {add,val,next};
	}
	for(int i = s1; i != -1; i=nodes[i].next){
		l1.push_back(nodes[i]);
	}
	for(int i = s2; i != -1; i=nodes[i].next){
		l2.push_back(nodes[i]);
	}
	vector<node> temp;
	if(l1.size() < l2.size()){
		temp = l1;
		l1 = l2;
		l2 = temp;
	}
	reverse(l2.begin(),l2.end());
	for(int i = 0 ; i < l1.size(); i++){
		ans.push_back(l1[i]);
		if(i % 2 == 1 && (i/2)<l2.size()){
			ans.push_back(l2[i/2]);
		}
	}
	for(int i = 0; i < ans.size(); i++){
		if(i == ans.size() - 1) printf("%05d %d -1",ans[i].add,ans[i].val);
		else printf("%05d %d %05d\n",ans[i].add,ans[i].val,ans[i+1].add);
	}
	return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值