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 ...
随机推荐
- mysql多实例-主从复制安装
安装环境:Centos6.5 mysql版本:mysql-5.5.32.tar.gz 一:安装前准备: 1.安装一些依赖库 yum install cmake gcc gcc-c++ ncurses- ...
- 2017上海QCon之旅总结(上)
本来这个公众号的交流消息中间件相关的技术的.这周去上海参加了QCon,第一次参加这样的技术会议,感受挺多的,所以整理一下自己的一些想法接公众号和大家交流一下. 下面进入正题,从自己参加了的一些分享中挑 ...
- C#基本功之泛型
一.没有泛型之前 在没有泛型之前,我们是怎么处理不同类型的相同操作的: 示例1 //下面是一个处理string类型的集合类型 public class MyStringList { string[] ...
- Apache常用配置
Apache配置文件:conf/httpd.conf.(注意:表示路径时使用‘/’而不使用‘\’,注释使用‘#’) 1. ServerRoot:服务器根目录,也就是Apache的安装目录,其他的目录配 ...
- Leetcode题解(十九)
54.Spiral Matrix 题目: 题目意思很简单,就是螺旋式访问矩阵元素.也没有比较经典的算法可以解决此题,只需要模拟一下这个过程即可. 代码如下: class Solution { publ ...
- 暑假练习赛 006 E Vanya and Label(数学)
Vanya and LabelCrawling in process... Crawling failed Time Limit:1000MS Memory Limit:262144KB ...
- Linux中常见问题(磁盘 定时任务)
第1章 linux无法上网 1) 第一步,先ping域名. ping www.baidu.com 2)再ping一个公网ip , ping 223.5.5.5/223.6.6.6/114.11 ...
- Nginx HTTP 核心模块
原文链接:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=17238776&id=2982697aio 语法:aio [ ...
- 乐视(LeTV)占用8080端口
- Java面试题解构
有次一个同事让我一同去面试一个候选人,没仔细看简历,所以在问了设计模式之后就让他谈一谈对内存泄漏和垃圾回收的理解,当时候选人一下子就懵了.后来才知道,他面的是初.中级开发职位,想来估计候选人心里也在骂 ...