【PAT】1074. Reversing Linked List(又是迷惑的一题)

【PAT】1074. Reversing Linked List(又是迷惑的一题)

题目:

1074 Reversing Linked List (25 分)

Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K=3, then you must output 3→2→1→6→5→4; if K=4, you must output 4→3→2→1→5→6.

Input Specification:

Each input file contains one test case. For each case, the first line contains the address of the first node, a positive N (≤105) which is the total number of nodes, and a positive K (≤N) which is the length of the sublist to be reversed. 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 an integer, and Next is the position of the next node.

Output Specification:

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

Sample Input:

00100 6 4
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218

Sample Output:

00000 4 33218
33218 3 12309
12309 2 00100
00100 1 99999
99999 5 68237
68237 6 -1

坑:

  • 我已经躲开了5位数
  • 但是!有脏数据,,,一开始真没想到这个题还有没用的数。
  • 好吧,纯脏数据,也就是上来就是-1 真难搞,pta没法看那数据点。烦银
  • 哎,卡在18分,菜鸡

小结&收获:

  •     while (first != -1) {        
            list[sum++] = first;
            first = next[first];
        }
    这个代码很好看
    
  • 关于是否构建结构体,还是用数组,我觉得如果要进行排序等整体移动,就用结构体,只是查找输出的话,数组会比较轻便

柳神的ac代码:

#include <iostream>
using namespace std;
int main() {
    int first, k, n, sum = 0;
    cin >> first >> n >> k;
    int temp, data[100005], next[100005], list[100005], result[100005];
    for (int i = 0; i < n; i++) {
        cin >> temp;
        cin >> data[temp] >> next[temp];
    }
    while (first != -1) {
        list[sum++] = first;
        first = next[first];
    }
    for (int i = 0; i < sum; i++) result[i] = list[i];
    for (int i = 0; i < (sum - sum % k); i++)
        result[i] = list[i / k * k + k - 1 - i % k];
    for (int i = 0; i < sum - 1; i++)
        printf("%05d %d %05d\n", result[i], data[result[i]], result[i + 1]);
    printf("%05d %d -1", result[sum - 1], data[result[sum - 1]]);
    return 0;
}

我的蒟蒻代码:

package com.pta.week03;

import java.lang.reflect.Array;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Scanner;

public class P1074ReversingLinkedList {
	static node arr []=new node [100010];
	static node cun [];
	
	static class node{
		int begin;
		int key;
		int end;
		public node(int begin, int key, int end) {
			super();
			this.begin = begin;
			this.key = key;
			this.end = end;
		}
	}
	public static void main(String[] args) {
		Scanner sc= new Scanner(System.in);
		int start=sc.nextInt();
		int n= sc.nextInt();
		int K=sc.nextInt();
		cun=new node[n];
		ArrayList<node> list = new ArrayList<>();
		for (int i = 0; i < n; i++) {
			int begin=sc.nextInt();
			int key=sc.nextInt();
			int end=sc.nextInt();
			arr[begin]=new node(begin, key, end);
		}
		int flag= 1;
		int count=0;
		for (int i = start; i !=-1; i=arr[i].end) {//嘻嘻
			cun[count]=arr[i];
			if(flag==K){
				for (int j = 0; j < K; j++) {
					list.add(cun[count-j]);
				}
				flag=1;
			}
			count++;
			flag++;
		}
		for (int i = count-flag+2; i < count; i++) {
			list.add(cun[i]);
		}
		out_put(list);
	}
	private static void out_put(ArrayList<node> list) {
		DecimalFormat df = new DecimalFormat("00000");
		for (int i = 0; i < list.size(); i++) {
			if(i!=list.size()-1){
				System.out.println(df.format(list.get(i).begin)+" "+list.get(i).key+" "+df.format(list.get(i+1).begin));
			}else {
				System.out.println(df.format(list.get(i).begin)+" "+list.get(i).key+" "+"-1");
			}
		}
		
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值