PAT (Basic Level)1025. 反转链表

本文介绍一种算法,用于实现链表中每K个结点的反转操作,并提供了一个具体的C++实现示例。

http://www.patest.cn/contests/pat-b-practise/1025

题目描述:

给定一个常数K以及一个单链表L,请编写程序将L中每K个结点反转。例如:给定L为1→2→3→4→5→6,K为3,则输出应该为3→2→1→6→5→4;如果K为4,则输出应该为4→3→2→1→5→6,即最后不到K个元素不反转。

输入格式:

每个输入包含1个测试用例。每个测试用例第1行给出第1个结点的地址、结点总个数正整数N(<= 105)、以及正整数K(<=N),即要求反转的子链结点的个数。结点的地址是5位非负整数,NULL地址用-1表示。

接下来有N行,每行格式为:

Address Data Next

其中Address是结点地址,Data是该结点保存的整数数据,Next是下一结点的地址。

输出格式:

对每个测试用例,顺序输出反转后的链表,其上每个结点占一行,格式与输入相同。

输入样例:
00100 6 4
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218
输出样例:
00000 4 33218
33218 3 12309
12309 2 00100
00100 1 99999
99999 5 68237
68237 6 -1
唉呀妈呀,这题整了一晚上。由于地址是五位整数格式,所以,我们可以开一个很大的数组,以空间换时间。地址的值就是该节点在数组中的索引。然后我是增加了一个previous node属性,便于count计数到K,也就是要反转的链末时,可以回溯。这样,只需要O(n)的时间。但是要注意输出中的next值是要改成反转后链的下一个节点的地址,尤其是注意每一段的最后一个地址。


#include <iostream>
#include <string>
#include <algorithm> 
#include <cmath>
#include <iomanip>
#include <ctype.h>
using namespace std;

#define max 100000

struct node {
	int addr ;
	int data ;
	int next ;
	int pre ;
};

node nodes[max] ;

string intout (int n)
{
	if (n==-1) return "-1";
	string s = "";
	for (int i = 10000 ; i>=10 ; i/=10)
	{
		char ch = n/i+'0' ;
		n %= i ;
		s += ch ;
	}
	char ch = n+'0' ;
	s += ch ;
	return s;
}

int main()
{
	int head = 0 ;
	int K = 0, N = 0;
	cin >> head >> N >> K ;
	while (N--){
		int ad = 0 ;
		cin >> ad ;
		nodes[ad].addr = ad ;
		cin >> nodes[ad].data >> nodes[ad].next ;
	}
	
	int current = head ;
	int nextAd = -1;
	int count = 1 ;
	int tail = head ;
	bool flag = 0;
	while ( current != -1)
	{
		if (count == K)//need to return
		{
			tail = nodes[current].next ;
			int temp = current ;
			if (flag==1) printf ("%s\n", intout(current).c_str()) ;
			while (count)
			{
				if (count==1 )
				{
					flag = 1;
					printf ("%s %d ", intout(nodes[temp].addr).c_str(), nodes[temp].data) ;
				 } 
				else printf ("%s %d %s\n", intout(nodes[temp].addr).c_str(), nodes[temp].data, intout(nodes[temp].pre).c_str()) ;
				temp =  nodes[temp].pre ;
				count--;
			}
		}
		nextAd = nodes[current].next ;
		if (nextAd != -1) nodes[nextAd].pre = current ;
		current = nextAd ;
		count ++ ;
	}
	printf("%s\n" , intout(tail).c_str());
	while (tail != -1)
	{
		printf ("%s %d %s\n", intout(nodes[tail].addr).c_str(), nodes[tail].data, intout(nodes[tail].next).c_str()) ;
		tail = nodes[tail].next ;
	} 
	if (head == -1) printf("-1");
	
	return 0;
}

/*
1 8 3
1 2 2
2 4 3
3 5 4
4 3 5
5 5 6
6 6 7
7 7 8
8 8 -1

*/


### 关于 PAT 1025 反转链表测试点6 的解决方案 针对PAT 1025反转链表中的测试点6,该测试点通常涉及较为复杂的边界条件或是大数据量的情况。为了确保程序能够通过此测试点,需特别注意数据结构的选择以及算法效率。 对于链表的操作,在C++环境下利用`map`可以有效地管理节点地址与节点信息之间的映射关系[^1]。具体来说: - 使用 `std::map<string, Node>` 来存储每个节点的信息,其中键为节点的十六进制地址,值则是一个自定义的数据结构Node,用于保存节点的具体属性(如数据域和指针域)。 当处理输入时,应先读取所有节点并存入上述提到的地图中。之后按照题目要求对这些节点进行排序操作——依据给定顺序重新排列它们,并更新各个节点间的链接指向。最后遍历已排序好的列表输出结果即可满足题目的需求。 值得注意的是,在实现过程中还需考虑一些特殊情况下的逻辑分支,比如空链表、单个元素组成的链表等情形下应该如何响应。此外,由于本题涉及到大量的内存分配及释放工作,因此建议尽可能减少不必要的对象创建次数以提高性能表现。 ```cpp #include <iostream> #include <vector> #include <algorithm> using namespace std; struct ListNode { string address; int data; string next; }; bool compare(const ListNode &a, const ListNode &b) { return a.address < b.address; } int main() { vector<ListNode> nodes; // Read input and store into the map. while (cin >> node.address >> node.data >> node.next && node.address != "-1") { nodes.push_back(node); } sort(nodes.begin(), nodes.end(), compare); for (size_t i = 0; i < nodes.size(); ++i){ if(i == nodes.size()-1 || nodes[i+1].address.empty()){ nodes[i].next = "-1"; }else{ nodes[i].next = nodes[i+1].address; } cout << nodes[i].address << " " << nodes[i].data << " " << nodes[i].next << endl; } return 0; } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值