Reversing Linked List (附有多余结点不在链表上Sample)

卡了好久,终于满分了,先上图
在这里插入图片描述

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

题目理解起来不难,需要注意的就是自己构造一个memory数组来当做内存,然后基本操作就和链表的操作一样的。

一直卡着的就是最后一个测试点“有多余结点不在链表上”,说实话这个提示给的并不是很明确,我想了好久才明白是什么意思。。。
这个测试点是指有的结点的address并没有被任何其他结点的next指向,这个结点的地址是被孤立的。
就像下面的例子
Sample Input

00100 6 3
00000 4 68237
00100 1 12309
68237 6 -1
33218 3 00000
12309 2 33218
99999 5 88888	//这个结点是被孤立的

它的正确输出应该是
Sample Output

33218 3 12309
12309 2 00100
00100 1 00000
00000 4 68237
68237 6 -1

大家可以拿这个sample测试自己的程序。

最后附上代码

#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>
#include<stdlib.h>

struct Memory
{
	int data;
	int next;
}memory[100001];

int main()
{

	//输入
	int head, n, k;
	scanf("%d%d%d", &head, &n, &k);
	int i;
	int add, dat, nex;
	for (i = 0; i < n; i++)
	{
		scanf("%d%d%d", &add, &dat, &nex);
		memory[add].data = dat;
		memory[add].next = nex;
	}


	//统计结点总数
	int num = 0;
	nex = head;
	while (1)
	{
		if (nex == -1)
		{
			break;
		}
		nex = memory[nex].next;
		num++;
	}


	//逆序
	nex = head;
	int revfirst = head;
	int revadd[100001];
	int revhead = 100000;
	int p, j, t;
	p = revhead;
	memory[revhead].next = head;
	for (t = 0; t < num / k; t++)	//num处使用n会导致“有多余结点不在链表上”
	{
		for (i = 0; i < k; i++)
		{
			revadd[i] = nex;
			nex = memory[nex].next;
		}

		for (j = k - 1; j >= 0; j--)
		{
			memory[p].next = revadd[j];
			p = revadd[j];
		}
		memory[p].next = nex;
	}


	//输出
	i = revhead;
	while (1)
	{
		i = memory[i].next;
		if (memory[i].next == -1)
		{
			printf("%05d %d %d\n", i, memory[i].data, memory[i].next);
			break;
		}
		else
		{
			printf("%05d %d %05d\n", i, memory[i].data, memory[i].next);
		}

	}

	return 0;
}

  • 6
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值