由于最近学的是线性结构,且因数组需开辟的空间太大。因此这里用的是纯链表实现的这个链表翻转。

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(<= 10^5​​) 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 NN 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 Nextis 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

 #include <stdio.h>
#include <stdlib.h> typedef struct Node
{
int address;
int data;
int nextAddress;
struct Node *next;
}Node;
typedef struct Node *LinkList; int main()
{
//排序前
LinkList L1, p1, q1;
L1 = (LinkList)malloc(sizeof(Node)); //创建头指针
L1->next = NULL;
int firstAddress;
int N, K;//N为总结点数 K为需翻转的数
scanf("%d %d %d", &firstAddress, &N, &K);
p1 = L1;
for(int i = ; i < N; i++) {
q1 = (LinkList)malloc(sizeof(Node));
scanf("%d %d %d",&q1->address, &q1->data, &q1->nextAddress);
p1->next = q1;
p1 = q1;
}
p1->next = NULL; // //测试没问题
// printf("测试1 :\n");
// p1 = L1->next;
// while(p1){
// printf("%05d %d %d\n", p1->address, p1->data, p1->nextAddress);
// p1 = p1->next;
// } //排序后
LinkList L2, p2;
L2 = (LinkList)malloc(sizeof(Node)); //创建头指针
L2->next = NULL;
int count = ;
int findAddress = firstAddress;
p2 = L2;
while(findAddress != -) { //while(count < N) {有多余结点不在链表上没通过 q1 = L1;
while(q1->next) {
if(q1->next->address == findAddress) {
p2->next = q1->next;
q1->next = q1->next->next;
p2 = p2->next;
count++;
// printf("count = %d\n",count);
findAddress = p2->nextAddress;
// printf("findAddress = %d\n",findAddress);
}else {
q1 = q1->next;
}
}
}
p2->next = NULL; // //测试没问题
// printf("测试2 :\n");
// p2 = L2->next;
// while(p2){
// printf("%05d %d %05d\n", p2->address, p2->data, p2->nextAddress);
// p2 = p2->next;
// }
//Reversing
LinkList L3, p3, q3, tail;
L3 = (LinkList)malloc(sizeof(Node)); //创建头指针
L3->next = NULL;
//将L2以头插法插入L3
int n = count; //防止有多余结点影响 n=N 会影响
int k = K;
p3 = L3;
p2 = L2;
while(n >= k) {
n -= k;
for(int i = ; i < k; i++) {
p3->next = p2->next;
p2->next = p2->next->next;
if(i == )
tail = p3->next;
else
p3->next->next = q3;
q3 = p3->next;
}
p3 = tail;
}
p3->next = L2->next; p3 = L3->next;
while(p3->next) {
printf("%05d %d %05d\n",p3->address, p3->data, p3->next->address);//不到五位数用0补全
p3 = p3->next;
}
printf("%05d %d -1\n",p3->address, p3->data);
return ;
}

----------------------------------------陈越老师讲解----------------------------------

单链表逆转模板代码

 LinkList Reverse (LinkList head, int K)    //单链表逆转K个结点 且仅一次
{
int cnt = ; //用于计数已逆转的结点数
LinkList new_ = head->next;
LinkList old = new_->next;
LinkList tmp;
while(cnt < K) {
tmp = old->next; //用于old结点逆转后 记录未逆转链表的头结点
old->next = new_; //逆转
new_ = old; //向后移位
old = tmp; //向后移位
cnt++; //逆转节点+1
}
head->next->next = old;
return new_; //新的头结点
}

02-线性结构2 Reversing Linked List的更多相关文章

  1. pat02-线性结构1. Reversing Linked List (25)

    02-线性结构1. Reversing Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, ...

  2. 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 ...

  3. 02-线性结构3 Reversing Linked List

    02-线性结构3 Reversing Linked List   (25分) 时间限制:400ms 内存限制:64MB 代码长度限制:16kB 判题程序:系统默认 作者:陈越 单位:浙江大学 http ...

  4. 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 ...

  5. 数据结构练习 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. PAT02-线性结构3 Reversing Linked List

    题目:https://pintia.cn/problem-sets/1010070491934568448/problems/1037889290772254722 先是看了牛客(https://ww ...

随机推荐

  1. Spark Standalone模式伪分布式环境搭建

    前提:安装好jdk1.7,hadoop 安装步骤: 1.安装scala 下载地址:http://www.scala-lang.org/download/ 配置环境变量: export SCALA_HO ...

  2. MFC学习 socket

    下面未处理异常 tcpserver.cpp #include "WinSock2.h" #include <stdio.h> #pragma comment(lib, ...

  3. 初探接口测试框架--python系列5

    点击标题下「蓝色微信名」可快速关注 坚持的是分享,搬运的是知识,图的是大家的进步,没有收费的培训,没有虚度的吹水,喜欢就关注.转发(免费帮助更多伙伴)等来交流,想了解的知识请留言,给你带来更多价值,是 ...

  4. Eclipse插件开发之基础篇(4) OSGi框架

    转载出处:http://www.cnblogs.com/liuzhuo. 1. 什么是OSGi框架 OSGi(Open Service Gateway Initiative)框架是运行在JavaVM环 ...

  5. memcached搭建缓存系统

    Memcached是danga.com(运营LiveJournal的技术团队)开发的一套分布式内存对象缓存系统,用于在动态系统中减少数据库负载,提升性能. 二.适用场合 1.分布式应用.由于memca ...

  6. nginx 编译模块说明

    --prefix= <path> - Nginx安装路径.如果没有指定,默认为 /usr/local/nginx. --sbin-path= <path> - Nginx可执行 ...

  7. tornado框架之路一

    Web 服务器 每个页面都以 HTML 的形式传送到你的浏览器中,HTML 是一种浏览器用来描述页面内容和结构的语言.那些负责发送 HTML 到浏览器的应用称之为“Web 服务器”,会让你迷惑的是,这 ...

  8. c++需要注意的地方和小算法

    C++11的标准 auto //可以自动类型, auto cars=//自动转化为int 强制转换 (long)thorn =long (thorn) //前者是c标准,后者是c++ 还有一种 sta ...

  9. VS/Visual studio 源代码编辑器里的空处出现点号解决办法

    此原因是不小心按错了键盘上的组合键Ctr+E+S, 再次按Ctr+E+S可消除.

  10. 洛谷P2724 联系 Contact

    P2724 联系 Contact 17通过 86提交 题目提供者该用户不存在 标签 难度普及/提高- 提交  讨论  题解 最新讨论 暂时没有讨论 题目背景 奶牛们开始对用射电望远镜扫描牧场外的宇宙感 ...