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 ...
随机推荐
- 初学者易上手的SSH-hibernate03 三大状态与缓存
这章主要来浅的学习下hibernate三大状态与缓存.首先来看下三大状态: 如上图,三大状态分别为临时状态(Transient),持久化状态(Persistent),游离状态(Detached).那么 ...
- phalcon——验证
一个完整的使用实例:(验证模型数据) use Phalcon\Mvc\Model; use Phalcon\Mvc\Model\Validator\Email as EmailValidator; u ...
- Java基础笔记13
1.集合与对象数组的区别 集合与对象数组共同点:都是存放对象的容器: 区别在于:①集合是没有长度限制的:②集合容器中没有类型的限制. 2.集合(都在Java.util包下) 常用的集合:Collect ...
- spark streaming 实例
spark-streaming读hdfs,统计文件中单词数量,并写入mysql package com.yeliang; import java.sql.Connection; import java ...
- 最长回文子串---Manacher算法
百度:Manacher算法 代码 #include <iostream> #include <string> #include <cstring> #include ...
- 集群配置虚拟主机及部署Hadoop集群碰到的问题
配置集群方案 Ubuntu下的配置apache虚拟主机方案: 对其中的Master节点配置虚拟主机,可以通过Chrome浏览器访问目录. 安装虚拟主机之前,先安装Apache2 sudo apt-ge ...
- Coins(多重背包+二进制优化)
Problem Description Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. On ...
- App网络管理
安卓开发一般都需要进行日志管理,常用操作老司机已为你封装完毕,你可以用这份工具进行管理,具体可以查看源码,现在为你开车,Demo传送门. 站点 系统日志输出工具类 → AppKeyBoardMgr g ...
- Struts2知识整理
准备找工作了.好忐忑!!! 整理整理知识,好好准备. 其实现在Struts2好像不是特别流行,不过还是有用武之地的. struts2简介 struts2是基于mvc开发模型的框架,属于表现层框架 核心 ...
- NHibernate 慎用Session.Merge
Session.Merge其意思有两个步骤, 一般用法: Session.Merge(obj); 1. 从当前的Session中获取obj对象, 如果未获取到则从数据库获取. 2. 把程序中的obj的 ...