02-线性结构3 Reversing Linked List
题目

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
基本思路
使用一个长度为99999的数组存放数据和next指针。最后进行反转。有两种方法:1.另建一个数组,存放要输出的数据,每次遍历反转个数个元素,并按相反顺序填入数组;2.把每个元素的next指针指向上一个元素,最后把原头结点的next指向新头结点的旧next。
代码
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
struct Node
{
int val;
int next;
};
int reverseList(Node a[],int header,int reverse);
int getLength(Node a[],int header);
int main()
{
Node a[100000];
int header, length, reverse=0;
cin >> header;
cin >> length;
cin >> reverse;
for (int i = 0; i < length; i++)
{
int pos;
scanf("%d", &pos);
scanf("%d %d", &a[pos].val, &a[pos].next);
}
int realLength=getLength(a, header);
//反转
int nowPos=header,nowHeader=0,newHeader=header,lastRear=0;
if(reverse>1)
{
for(int i=0;i<realLength/reverse;i++)
{
//第一次反转更新头结点
if(nowPos==header)
{
lastRear=nowPos;
nowHeader=reverseList(a,header,reverse);
newHeader=nowHeader;
nowPos=a[lastRear].next;
}
else
{
nowHeader=reverseList(a,nowPos,reverse);
a[lastRear].next=nowHeader;
lastRear=nowPos;
nowPos=a[lastRear].next;
}
}
}
//输出
int pos=newHeader;
while (pos!=-1)
{
printf("%05d %d ", pos, a[pos].val);
if (a[pos].next!= -1)
printf("%05d\n", a[pos].next);
else
printf("%d\n", a[pos].next);
pos = a[pos].next;
}
return 0;
}
int getLength(Node a[],int header)
{
int pos=header;
int length=0;
while (pos!=-1)
{
length++;
pos = a[pos].next;
}
return length;
}
int reverseList(Node a[],int header,int reverse)
{
int nowP=a[header].next,lastP=header;
//先将第2-reverse间的元素的next指向上一个元素
for(int i=0;i<reverse-1;i++)
{
if(nowP==-1)
break;
int newPos;
newPos=a[nowP].next;
a[nowP].next=lastP;
//更新位置
lastP=nowP;
nowP=newPos;
}
a[header].next=nowP;
header=lastP;
return header;
}
总结
第一次做时还是没审清题意,题目是要每K个元素反转,一开始只反转了一次。还有就是注意调用反转函数次数应该是链表的真实长度/反转长度,因为可能会有多余结点。
02-线性结构3 Reversing Linked List的更多相关文章
- pat02-线性结构1. Reversing Linked List (25)
02-线性结构1. Reversing Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, ...
- 02-线性结构3 Reversing Linked List(25 point(s)) 【链表】
02-线性结构3 Reversing Linked List(25 point(s)) Given a constant K and a singly linked list L, you are s ...
- 02-线性结构3 Reversing Linked List
02-线性结构3 Reversing Linked List (25分) 时间限制:400ms 内存限制:64MB 代码长度限制:16kB 判题程序:系统默认 作者:陈越 单位:浙江大学 http ...
- 02-线性结构2 Reversing Linked List
由于最近学的是线性结构,且因数组需开辟的空间太大.因此这里用的是纯链表实现的这个链表翻转. Given a constant K and a singly linked list L, you are ...
- PTA 02-线性结构3 Reversing Linked List (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/664 5-2 Reversing Linked List (25分) Given a ...
- 数据结构练习 02-线性结构2. 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 ...
- 02-线性结构3 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 ...
- 02-线性结构3 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 ...
- PAT02-线性结构3 Reversing Linked List
题目:https://pintia.cn/problem-sets/1010070491934568448/problems/1037889290772254722 先是看了牛客(https://ww ...
随机推荐
- 【转】地址空间、内核空间、IO地址空间
http://blog.csdn.net/wuxinke_blog/article/details/8769131 有这么一系列的问题,是否在困扰着你:用户程序编译连接形成的地址空间在什么范围内?内核 ...
- Python 的装饰器
Python 在语言级别提供了装饰器模式的实现,代码中Python内置的 functools.wraps 会完成包括函数名属性处理替换 #!/usr/bin/env python3 #--coding ...
- [mysql使用(1)] 64位Linux下安装mysql-5.7.13-linux-glibc2.5-x86_64
由于公司临时让将Oracle的数据移植到mysql上面,所以让我在公司服务器上面安装一下mysql.下面就是我的安装过程以及一些错误解决思路.其实对于不同版本安装大体都有差不多. 1. 从官网下载 m ...
- LeetCode 538. Convert BST to Greater Tree (把二叉搜索树转换成较大树)
Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original B ...
- NLog在asp.net中的使用
一.关于安装 1.可以直接通过vs自带的Nuget包管理器来搜索下载,直接搜索"NLog": 注意,除了要安装第一个之外,一定要安装"NLog.Config", ...
- swift 之 函数
swift的函数跟脚本语言有很多神似之处. 如果有一天用swift开发服务器 ,很期待哇(一切皆有可能,毕竟人家说要跑在Linux上),
- Turn the corner
Problem Description Mr. West bought a new car! So he is travelling around the city. One day he comes ...
- js中的浅复制和深复制
浅复制:浅复制是复制引用,复制后的引用都是指向同一个对象的实例,彼此之间的操作会互相影响 深复制:深复制不是简单的复制引用,而是在堆中重新分配内存,并且把源对象实例的所有属性都进行新建复制,以保证深复 ...
- BootStrap Table使用小结
1.在当前表格的最后新增数据 $("#data_module_table").bootstrapTable('append', data.data);//data.data---- ...
- thinkphp碰到的一些小问题
1. 生成的html自动被去掉换行和空格,压缩挤到一起了. 解决: 开启debug即可,在入口文件增加 define("APP_DEBUG",true); 2. 添加mysql的 ...