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

baixiaofei567發表於2020-11-29

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;
} 

相關文章