PAT (Grade A) 2019 autumn test 7-2 Merging Linked Lists (25 points) Ling Chen 1642

Keywords: Algorithm data structure linked list Simulation

PAT (Grade A) 2019 autumn test 7-2 Merging Linked Lists (25 points)

Title Description:

Given two singly linked lists L1=a1→a2→⋯→an−1→an and L2=b1→b2→⋯→bm−1→bm. If n≥2m, you are supposed to reverse and merge the shorter one into the longer one to obtain a list like a1→a2→bm→a3→a4→bm−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.

Given two single chain tables L1=a1 → a2 →... → an − 1 → an and L2=b1 → b2 →... → bm − 1 → bm. If n ≥ 2m, you should reverse and merge the shorter ones into the longer one to get a similar list a1 → a2 → bm → a3 → a4 → bm − 1. For example, given a linked list 6 → 7 and another linked list 1 → 2 → 3 → 4 → 5, you should 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 L1 and L2, plus a positive N (≤105) 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 105, 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.

Each input file contains a test case. For each case, the first line contains the address of the first node representing L1 and L2, plus a positive integer N (≤ 105) representing the total number of nodes. The address of a node is a 5-bit non negative number, and the empty node is represented by - 1.

Next, there are N lines. Each line describes a node in the following format:

Address Data Next

Address indicates the node address, Data is a positive integer no more than 105, and Next is the address of the Next node. The title ensures that the linked list is not empty, and the length of the long linked list is at least twice the length of the short linked list.

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.

For each case, the linked list of results is output in turn. Each node occupies one line, and the format is the same as the input format.

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

The Idea:

The title clearly states that the two linked lists are long and short, and the length n of the long linked list and the length m of the short linked list must meet n > = 2m

Points needing attention:

  • It does not indicate which is a long list and which is a short list
  • Not all nodes given are nodes on the linked list (there are invalid nodes and the last two test points)

My idea is to use the structure to simulate the linked list, and then because when merging, one is added every two, and the addition is in reverse order, you can choose to flip the short linked list first, but here I choose to use the stack, which is very convenient to take out the nodes to be added.

The Codes:

#include<bits/stdc++.h>
using namespace std ;
const int maxn = 100010 ;

struct node{
	int data , next ;
}link[maxn] ;

int l1 , l2 , n , id , ans ;

void merge(int start , stack<int> st){
	ans = start ; // Record the last merged starting address
	for(int i = 0 ; !st.empty() ; i ++){
		if(i > 0 && (i + 1) % 2 == 0){
			int top = st.top() ;
			st.pop() ;
			link[top].next = link[start].next ;
			link[start].next = top ;
			start = link[top].next ;
		}else start = link[start].next ;
	}
}
int main(){
	scanf("%d%d%d" , &l1 , &l2 , &n) ;
	
    for(int i = 0 ; i < n ; i ++){
		scanf("%d" , &id) ;
		scanf("%d%d" , &link[id].data , &link[id].next) ;
	}
	
    int p = l1 , q = l2 ;
	stack<int> st1 , st2 ;
	while(p != -1 && q != -1){ // Put all nodes of the short linked list on the stack
		st1.push(p) ;
		p = link[p].next ;
		st2.push(q) ;
		q = link[q].next ;
	}
    
	if(p == -1) merge(l2 , st1) ; // If l1 is a short chain, merge l1 into l2
	else merge(l1 , st2) ; // If l2 is a short chain, l2 is merged into l1
	
    while(link[ans].next != -1){ // Not n, because there are invalid nodes
		printf("%05d %d %05d\n" , ans , link[ans].data , link[ans].next) ;
		ans = link[ans].next ;
	}
	printf("%05d %d -1\n" , ans , link[ans].data) ;
	return 0 ;
}

Using the front and back relationship of array to simulate the front and back nodes of linked list is worth learning!!!

#include<bits/stdc++.h>
using namespace std;
struct node {
    int data, addr;
};
node nodes[100005];

int main() {
    int headA, headB, n;
    cin >> headA >> headB >> n;

    for (int i = 0; i < n; ++i) {
        int address, data, next;
        cin >> address >> data >> next;
        nodes[address] = {data, next};
    }

    vector<node> a, b, c;
    for (int p = headA; p != -1; p = nodes[p].addr) 
        a.push_back({nodes[p].data, p});
    for (int p = headB; p != -1; p = nodes[p].addr)
        b.push_back({nodes[p].data, p});

    if (a.size() < b.size()) swap(a, b);
    reverse(b.begin(), b.end());
    for (int i = 0, j = 0, k = 0; i < a.size() || j < b.size(); ++k) {
        if (k % 3 == 2 && j < b.size()) c.push_back(b[j++]);
        else c.push_back(a[i++]);
    }

    for (int i = 0; i < c.size(); ++i) {
        printf("%05d %d ", c[i].addr, c[i].data);
        if (i == c.size() - 1) cout << "-1\n";
        else printf("%05d\n", c[i + 1].addr);
    }
}

The result :

Posted by jwinn on Sat, 04 Sep 2021 13:38:14 -0700