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 ...
随机推荐
- 什么是GUID?
定义及格式 1.全局唯一标识符(GUID,Globally Unique Identifier)是一种由算法生成的二进制长度为128位的数字标识符.GUID主要用于在拥有多个节点.多台计算机的网络或系 ...
- 简述Apache的ab测试主要有那些关键指标
一.ab的原理 ab是apachebench命令的缩写. ab的原理:ab命令会创建多个并发访问线程,模拟多个访问者同时对某一URL地址进行访问.它的测试目标是基于URL的,因此,它既可以用来测试ap ...
- [Java第一课]环境变量的配置以及eclipse一些常用快捷键
1.环境变量的配置(这里对xp系统电脑来说:) 首先安装jdk软件. 然后在我的电脑(右键)-->属性-->高级-->环境变量-->系统变量(注意)-->新建(新建两个p ...
- PHP垃圾回收机制
一.引用计数基本知识 每个php变量存在一个叫"zval"的变量容器中,当一个变量被赋常量值时,就会生成一个zval变量容器.一个zval变量容器,除了包含变量的类型和值,还包括两 ...
- SQL语句查询表中的所有约束
select * from sysobjects where parent_obj in(select id from sysobjects where name='表名') 或者 exec sp_h ...
- 走进 Visual Studio Mobile Center for Xamarin.Forms
前几篇分别介绍了 Xamarin.Forms 的 MVVM 的 Prism,UITest,Nuint Test,那这样算下来,代码部分基本结构都有了(逻辑就先忽略吧) 那接下来就应该是自动 Build ...
- 暑假练习赛 006 B Bear and Prime 100
Bear and Prime 100Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:262144KB ...
- NumPy基础练习(练一遍搞定NumPy)
import numpy as np import pandas as pd from numpy import random from numpy.random import randn ##### ...
- js里面的map、filter、forEach、reduce、for in、for of等遍历方法
1.map 遍历数组,改变原数组 [2, 3, 4, 5].map(function(val, key,arr) { return val > 3; }) var data = [ { name ...
- JAVAWEB开发环境搭建,附JDK开发环境一键配置批处理bat
JDK配置: CLASSPATH: .;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar JAVA_HOME: C:\Program Files\J ...