1025 反转链表

题目传送门
核心代码:
//数组模拟链表
while (first != -1)
{
list[sum++] = first;
first = next[first];
}

#include <iostream>
#include <algorithm>
using namespace std;
int main() 
{
	int first, k, n, temp;
	cin >> first >> n >> k;
	int data[100005], next[100005], list[100005];
	for (int i = 0; i < n; i++) 
	{
		cin >> temp;
		cin >> data[temp] >> next[temp];
	}
	int sum = 0;
	while (first != -1) 
	{
		list[sum++] = first;
		first = next[first];
	}
	for (int i = 0; i < (sum - sum % k); i += k)
	{
		reverse(begin(list) + i, begin(list) + i + k);
	}
	for (int i = 0; i < sum - 1; i++)
	{
		printf("%05d %d %05d\n", list[i], data[list[i]], list[i + 1]);
	}
	printf("%05d %d -1", list[sum - 1], data[list[sum - 1]]);
	return 0;
}
1025 反转链表的Python代码如下所示: ```python # 开始的地址,链表节点数量,K start_address, count, k = list(map(int, input().split(' '))) # 创建一个长度为10万的空列表 single_lists = [None * 100000 # 读取输入,并存放至空列表中 for i in range(count): single_list = input().split(' ') single_lists[int(single_list = reverse_result_lists[::-1] # 按要求输出单链表 for j in range(count - 1): result_lists[j][2 = result_lists[j + 1][0] print(' '.join(result_lists[j])) result_lists[-1][2 = '-1' print(' '.join(result_lists[-1])) ``` 以上代码实现了反转链表的功能,通过读取输入的链表节点,并按照题目要求进行反转和输出。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [(python 3)1025 反转链表 (25分)](https://blog.csdn.net/qq_40728667/article/details/107581997)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [1025 反转链表 (python)](https://blog.csdn.net/qq_55322766/article/details/122285032)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值