PAT(甲級)2019年秋季考試 7-2 Merging Linked Lists

7-2 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.node

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.git

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.spa

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.3d

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

題目限制:

image.png

題目大意:

給定兩個鏈表,如今須要將短的鏈表按照以下的方法插入到長的鏈表中,首先將短鏈表逆序,而後每隔兩個節點插入到長的鏈表中去。code

算法思路:

咱們使用list1和list2分別保存兩個鏈表,這裏假設list1保存的是短的,另一種狀況同樣,使用result保存全部插入完畢的鏈表,其實能夠分2種情形考慮這個問題,第一種就是list1還有節點須要插入,那麼就把插入2個list2節點和1個list1節點當作一個原子操做,第二種狀況就是list1沒有節點插入了,那麼就依次插入list2節點,直到完畢便可。具體作法以下:orm

  • 一、使用index_list1 = list1.size()-1,index_list2 = 0,分別表示目前list1和list2的待插入位置
  • 二、只要index_list2<list2.size(),就說明插入過程沒有結束轉3,不然轉5
  • 三、若是index_list1>=0,說明list1還有節點,插入2個list2節點和1個list1節點,轉2
  • 四、若是index_list1<0,直接插入list2節點,轉2
  • 五、輸出全部的result,每個節點的next即爲後一節點的address。

提交結果:

image.png

AC代碼:

#include<cstdio>
#include<vector>

using namespace std;

struct Node{
    int address;
    int data;
    int next;
}all[100000];
int num = 0;

vector<Node> list1;
vector<Node> list2;
vector<Node> result;

int main(){
    int begin1,begin2,N;
    scanf("%d %d %d",&begin1,&begin2,&N);
    Node node;
    for(int i=0;i<N;++i){
        scanf("%d %d %d",&node.address,&node.data,&node.next);
        all[node.address] = node;
    }
    while(begin1!=-1){
        list1.push_back(all[begin1]);
        begin1 = all[begin1].next;
    }
    while(begin2!=-1){
        list2.push_back(all[begin2]);
        begin2 = all[begin2].next;
    }
    if(list1.size()<list2.size()){
        // list2更長
        int index_list1 = list1.size()-1;
        int index_list2 = 0;
        while(index_list2<list2.size()){
            if(index_list1>=0){
                // 若是list1還有節點
                for(int i=0;i<2;++i){
                    result.push_back(list2[index_list2]);
                    ++index_list2;
                }
                result.push_back(list1[index_list1]);
                --index_list1;
            }else{
                result.push_back(list2[index_list2]);
                ++index_list2;
            }
        }
    }else{
        // list1更長
        int index_list1 = 0;
        int index_list2 = list2.size()-1;
        while(index_list1<list1.size()){
            if(index_list2>=0){
                // 若是list2還有節點
                for(int i=0;i<2;++i){
                    result.push_back(list1[index_list1]);
                    ++index_list1;
                }
                result.push_back(list2[index_list2]);
                --index_list2;
            }else{
                result.push_back(list1[index_list1]);
                ++index_list1;
            }
        }
    }
    for(int i=0;i<result.size();++i){
        printf("%05d %d ",result[i].address,result[i].data);
        if(i!=result.size()-1){
            printf("%05d\n",result[i+1].address);
        }else{
            printf("-1\n");
        }
    }
    return 0;
}
相關文章
相關標籤/搜索