1074 Reversing Linked List (25 分) java 题解
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
解题思路:
题目大意为给定一个链表,将链表上的每n个元素反转。(部分反转),并输出最终链表。
读取所给链表后,按顺序装入list中,求出需要反转的次数(要用list.size()求,有无效结点),利用双层for处理反转,外层控制次数,内层将每轮的处理元素装入list2中,用list.remove()方便,再将list中的剩余元素装入list2,最后处理结点间关系。
// 处理结点间关系
temp = -1;
for(int i = list2.size() - 1;i >= 0;i--) {
list2.get(i).next = temp;
temp = list2.get(i).adress;;
}
java代码(超时):
import java.io.*;
import java.util.*;public class Main {public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new InputStreamReader(System.in));String[] split = br.readLine().split(" ");int adressStart = Integer.parseInt(split[0]);int n = Integer.parseInt(split[1]);int before = Integer.parseInt(split[2]);Node1074 arr[] = new Node1074[100005];for(int i = 0; i < n;i++) {split = br.readLine().split(" ");int adress = Integer.parseInt(split[0]);int data = Integer.parseInt(split[1]);int next = Integer.parseInt(split[2]);arr[adress] = new Node1074(adress, data, next);}int temp = adressStart;List<Node1074> list = new ArrayList<Node1074>();while(temp != -1) {//按链表顺序存入list中list.add(arr[temp]);temp = arr[temp].next;}int count = list.size() / before;//求需要反转几次List<Node1074> list2 = new ArrayList<Node1074>();for(int j = 1; j <= count ;j++) {//反转次数for(int i = before * j - 1;i >= before*(j - 1) ;i--) {list2.add(list.get(i));}}for(int i = before * count; i < list.size();i++) {//将剩余元素加入list2中list2.add(list.get(i));}temp = -1;for(int i = list2.size() - 1;i >= 0;i--) {//调整地址list2.get(i).next = temp;temp = list2.get(i).adress;;}for(Node1074 data : list2) {//输出System.out.println(data);}}
}
class Node1074{int adress;int data;int next;public Node1074(int adress, int data, int next) {this.adress = adress;this.data = data;this.next = next;}@Overridepublic String toString() {if(this.next != -1)return String.format("%05d", adress) + " " + data + " " + String.format("%05d", next);elsereturn String.format("%05d", adress) + " " + data + " " + String.format("%02d", next);}
}
PAT提交截图:
1074 Reversing Linked List (25 分) java 题解相关推荐
- 1074 Reversing Linked List (25 分)【难度: 一般 / 知识点: 链表】
https://pintia.cn/problem-sets/994805342720868352/problems/994805394512134144 乙级里面的原题吧,就用哈希表建链表,reve ...
- 1080 MOOC期终成绩 (25 分) java 题解
题目描述: 对于在中国大学MOOC(http://www.icourse163.org/ )学习"数据结构"课程的学生,想要获得一张合格证书,必须首先获得不少于200分的在线编程作 ...
- PAT 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 elem ...
- 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 elem ...
- 1074. Reversing Linked List (25)-PAT甲级真题
Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...
- 1007 Maximum Subsequence Sum (25 分) java 题解
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { N ...
- 1063 Set Similarity (25 分) java 题解
Given two sets of integers, the similarity of the sets is defined to be Nc/Nt×100%, where Nc is t ...
- L2-031 深入虎穴 (25 分) Java题解 (树的最大深度dfs,bfs)
输入样例: 13 3 2 3 4 2 5 6 1 7 1 8 1 9 0 2 11 10 1 13 0 0 1 12 0 0 输出样例: 12 解题思路: 找最大深度的编号,由于答案唯一,所以宽搜到的 ...
- 02-线性结构3 Reversing Linked List (25 分)
如果 单纯从做题角度来讲,上一个做法当然没问题,可是目的是为了练习静态链表,so还是不要投机取巧了,静态链表解法如下: #include <stdio.h> struct data {in ...
最新文章
- eureka java_spring cloud 入门系列二:使用Eureka 进行服务治理
- word转换成pdf java代码_java代码实现word转换成pdf
- 【SAS NOTE】substr函数
- java多线程中的死锁、活锁、饥饿、无锁都是什么鬼?
- LeetCode 560. 和为K的子数组(前缀和差分)
- Hadoop源代码分析(MapReduce概论)
- python中正则表达式的默认匹配方式为_Python模式匹配与正则表达式
- java源程序编译型_Java语言的源程序不是编译型的,而是编译解释型的。
- SCOM2012功能测试(18)—对象发现(替代)
- 12年外贸婚纱跨境老司机分享独立站推广引流实操干货
- python网络爬虫系列教程——Scrapy框架应用全解
- 【C语言】break,continue的区别
- 怎么看R语言是不是在运行_生信技能树R语言视频课听后感 (10万+的播放量就看这个春节)...
- kafka 权威指南笔记
- 软件测试方法-测试用例
- mongodb数据的导入导出备份恢复
- 墓碑上的字符C语言,墓碑上常见的“故显考、故显妣、先考、先妣”,分别是什么意思?...
- 重置IE:专治IE疑难杂症的“万精油”(转)
- 神策数据虚席以待,欢迎加入!
- APICloud:让开发移动应用像拼积木一样简单