02-1 Reversing Linked List (PAT)

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 (<= 105) 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 N 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 Next is 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

1.使用递归求解

2.考虑输入中包含孤立节点的情况

 1 #include <iostream>
 2 #include <string>
 3 #include <stdlib.h>
 4 #include <iomanip>
 5 using namespace std;
 6
 7 typedef struct{
 8     int data;
 9     int next;
10 } dataNode;
11
12 int ReverseList( dataNode node[], int numNode, int lenSub, int firAdd );
13
14 int main()
15 {
16     //输入处理
17     int firAdd, numNode, lenSub;
18     //第一行处理
19     cin >> firAdd >> numNode >> lenSub;
20     dataNode nodes[100000];
21     int i;
22     int addr, data, next, head;
23     for ( i = 0; i < numNode; i++ )
24     {
25         cin >> addr >> data >> next;
26         nodes[ addr ].data = data;
27         nodes[ addr ].next = next;
28     }
29     //排除无效节点,得到有效节点数量
30     int noNode = firAdd;
31     int realNum = 0;
32     while( noNode != -1 )
33     {
34         noNode = nodes[ noNode ].next;
35         realNum++;
36     }
37     head = ReverseList( nodes, realNum, lenSub, firAdd );
38     while ( head != -1 )
39     {
40         cout << setw(5) << setfill(‘0‘) << head << ‘ ‘;
41         cout << nodes[ head ].data << ‘ ‘;
42         if ( nodes[ head ].next == -1 )
43         {
44             cout << nodes[ head ].next << endl;
45         }
46         else
47         {
48             cout << setw(5) << setfill(‘0‘) << nodes[ head ].next << endl;
49         }
50
51         head = nodes[ head ].next;
52     }
53     //输出处理
54     return 0;
55 }
56
57 int ReverseList( dataNode nodes[], int numNode, int lenSub, int firAdd )
58 {
59     if ( lenSub > numNode || nodes == NULL || firAdd == -1 ) return firAdd;
60     int current = firAdd;
61     int prev = -1;
62     int next = -1;
63     int count = 0;
64     while ( current != -1 && count < lenSub )
65     {
66         next = nodes[ current ].next;
67         nodes[ current ].next = prev;
68         prev = current;
69         current = next;
70         count++;
71     }
72     if ( next != -1 )
73     {
74         nodes[ firAdd ].next = ReverseList( nodes, numNode - lenSub, lenSub, next );
75     }
76     return prev;
77 }
时间: 2024-08-27 16:52:14

02-1 Reversing Linked List (PAT)的相关文章

02-线性结构3 Reversing Linked List (25 分)

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

Java基础02 方法与数据成员(转载)

对象中的数据成员表示对象的状态.对象可以执行方法,表示特定的动作. 此外,我们还了解了类(class).同一类的对象属于相同的类型(type).我们可以定义类,并使用该定义来产生对象. 调用同一对象的数据成员 方法可以调用该对象的数据成员.比如下面我们给Human类增加一个getHeight()的方法.该方法返回height数据成员的值: public class Test{    public static void main(String[] args){        Human aPer

企业边界网络设备的一般配置:ACL、端口复用(PAT)、端口映射

一.概述: 企业的边界网络设备一般是路由器或者多层交换机,主要实现的功能如下:(1)实现内网部分设备访问外网:(2)客户从公网访问企业内网的Web服务器等:(3)运维人员从外网访问企业内部的部分设备进行远程维护.其中第一项功能需求通过ACL和端口复用(PAT)技术实现,第二.三项功能需求通过端口映射技术实现. 本文结合拓扑图讲述上述几项功能的实现技术及具体配置. 二.拓扑图说明: 如上图所示,绿色背景部分为企业内部网络环境(COMPANY-Network),蓝色背景部分为运营商网络环境(ISP-

Tensoflw.js - 02 - 模型与内存管理(易懂)

Tensoflw.js - 02 - 模型与内存管理(易懂) 参考 W3Cschool 文档:https://www.w3cschool.cn/tensorflowjs/ 本文主要翻译一些英文注释,添加通俗的注释,记录新手使用遇到的小问题,去除不必要的部分,帮助新手快速入门 上一篇介绍了,Tensorflow.js 的安装,张量与变量的表示方法.创建和输出 Tensoflw.js - 01 - 安装与入门(中文注释) 本篇介绍模型与内存管理 Tensorflow.js 模型: 1.在 Tenso

终结Linked List(三)

第一篇终结Linked List(一).终结Linked List(二)主要讲了单链表的基础知识,接下来的第二篇主要讲一些比较经典的问题. 一.Count() 给一个单链表和一个整数,返回这个整数在链表中出现了多少次. /* Given a list and an int, return the number of times that int ocucurs in the list. */ int Count(struct node* head,int searchFor) { int cnt

leetcode_234题——Palindrome Linked List(链表)

Palindrome Linked List Total Accepted: 5466 Total Submissions: 23472My Submissions Question Solution Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time and O(1) space? Hide Tags Linked List Two Pointer

终结Linked List(二)

三.编码技巧 1.遍历链表 先将head指针赋值给一个局部变量current: //return the number of nodes in a list (while-loop version) int Length(struct node* head) { int count = 0; struct node* current = head; while (current != NULL) { count++; current = current->next; } return count

pat(A)1074. Reversing Linked List(哈希)

1.没看到reverse the links of every K elements on L这一句,WA到死. 2.代码: #include<iostream> #include<cstring> #include <algorithm> #include<cstdio> using namespace std; struct Node { int d; int next; void init(int x,int y) { d=x; next=y; } }

1074 Reversing Linked List (25 分)

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: