1074 Reversing Linked List 测试点5、6

测试点5:每k个反转一次,不足k的不反转。错误原因:只有第一组k个反转了,而后的几组都没反转

测试用例:

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

输出

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

测试点6:错误原因:有些节点不在头节点指向的链表中,不能算到其中

测试用例:

00000 6 3
00000 1 11111
11111 2 22222
22222 3 -1
33333 4 44444
44444 5 55555
55555 6 -1

输出:

22222 3 11111
11111 2 00000
00000 1 -1

完整代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
#include <map>
#include <vector>
#include <set>
#include <stack>
#include <queue>
using namespace std;
const int maxn = 100010;
const int INF = 100000;
int first,m,n;
struct Node{
	int data,next;
}list[maxn];
//输入需要反转串前一个节点的地址,以及要反转的长度,返回下一个反转串前一个节点的地址
int reverse(int beforeFirst,int length){
	int head=beforeFirst,nexta;
	int a=list[beforeFirst].next;
	int first = list[beforeFirst].next;
	int end = a;
	while(length>0){
		nexta=list[a].next;
		list[a].next = list[head].next;
		list[head].next = a;
		a = nexta;
		length--;
	}
	list[first].next = a;
	return end;
}
int main()
{
	scanf("%d%d%d",&first,&n,&m);
	for(int i=0;i<n;i++){
		int id;
		scanf("%d",&id);
		scanf("%d%d",&list[id].data,&list[id].next); 
	}
	int now = first,num=0;
    //测试点6,确保在头节点链表内的数据参与运算
	while(now!=-1){
		num++;
		now = list[now].next;
	}
	if(num==0) return 0;
	list[INF].next = first;
	int time = num/m-1;
	int before=reverse(INF,m);
    //测试点5,多组反转
	for(int i=0;i<time;i++){
		before = reverse(before,m);
	}
	
	now = list[INF].next;
	while(now!=-1){
		printf("%05d %d ",now,list[now].data);
		now = list[now].next;
        //易错点,-1不能用%05d输出
		if(now==-1) printf("-1\n");
		else printf("%05d\n",now);
	}
	return 0;
}

方法2

#include <cstdio>
#include <cstring>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
#include <map>
#include <vector>
#include <set>
#include <stack>
#include <queue>
using namespace std;
const int maxn = 100010;
queue<int> q;
struct Node{
	int data,next;
}list[maxn];
int main()
{
	int first,num=0,n,len,start,pre=100000,data,next=0;
	scanf("%d%d%d",&first,&num,&len);
	list[pre].next = first;
	bool flag = true;
	for(int i=0;i<num;i++){
		int address;
		scanf("%d%d%d",&address,&data,&next);
		list[address].data = data;
		list[address].next = next;
	}
	int now = first;
	num=0;
	while(now!=-1){
		now = list[now].next;
		num++;
	}
	now = first;
	int firstAll,t;
	int time = num/len;
	int nownext;
	for(int i=0;i<time;i++){
		for(int j=0;j<len;j++){
			int temp = list[pre].next;
			//printf("temp:%d\n",temp);
			list[pre].next  = now;
			nownext = list[now].next;
			list[now].next = temp;
			if(i==0){
				firstAll = now;
				//printf("first%d",firstAll);
			} 
			t = now;
			now=nownext;
		}
		list[first].next = now;
		pre = first;
		first = now;
	}
	for(int i=0;i<num;i++){
		printf("%05d %d ",firstAll,list[firstAll].data,list[firstAll].next);
		if(list[firstAll].next!=-1) printf("%05d\n",list[firstAll].next);
		else printf("-1\n");
		firstAll = list[firstAll].next;
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值