7-10 重排链表 (25分)

7-10 重排链表 (25分)

给定一个单链表 L​1​​ →L​2​​ →⋯→L​n−1​​ →L​n​​ ,请编写程序将链表重新排列为 L​n​​ →L​1​​ →L​n−1​​ →L​2​​ →⋯。例如:给定L为1→2→3→4→5→6,则输出应该为6→1→5→2→4→3。

输入格式:

每个输入包含1个测试用例。每个测试用例第1行给出第1个结点的地址和结点总个数,即正整数N (≤10e​5​​ )。结点的地址是5位非负整数,NULL地址用−1表示。

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

Address Data Next

其中Address是结点地址;Data是该结点保存的数据,为不超过10e​5​​ 的正整数;Next是下一结点的地址。题目保证给出的链表上至少有两个结点。

输出格式:

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

输入样例:

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

输出样例:

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

AC

#include<bits/stdc++.h>
#include <iostream>
#include <algorithm>
using namespace std;

struct stu {
	int data;
	int next;
};
stu x[100001];
int main() {
	int h,n;
	cin>>h>>n;
	int a,b,c;
	for(int i=0; i<n; i++) {
		cin>>a>>b>>c;
		x[a].data=b;
		x[a].next=c;
	}
	int p[n+1],q=0;
	for(int i=h; i!=-1; i=x[i].next) {
		p[q++]=i;
	}
	int i=0,j=q-1;//这里原来的是j=n-1  改正之后就对了 不知道为什么 
	while(i<=j) {
		if(j==i||j-i==1) {
			if(j==i)
				printf("%05d %d -1\n",p[i],x[p[i]].data);
			else {
				printf("%05d %d %05d\n",p[j],x[p[j]].data,p[i]);
				printf("%05d %d -1\n",p[i],x[p[i]].data);
			}
		} else {
			printf("%05d %d %05d\n",p[j],x[p[j]].data,p[i]);
			printf("%05d %d %05d\n",p[i],x[p[i]].data,p[j-1]);
		}
		i++;
		j--;
	}
	return 0;
}

写错的题目

#include<bits/stdc++.h>
using namespace std;

struct node {
	int address;
	int data;
	int next;
};
node a[99999];
int main() {

	int head,n;
	cin>>head>>n;
	for(int i=0; i<n; i++) {
		cin>>a[i].address>>a[i].data>>a[i].next;
	}

	int b[n];//记录正确的地址顺序
	int c[n];//记录正确地址所在数组的下标
	b[0]=head;
	int j=0;
	while(b[j]!=-1) {
		for(int i=0; i<n; i++) {
			if(a[i].address==b[j]) {
				c[j]=i;
				b[++j]=a[i].next;
				break;
			}
		}
	}

	int i=0,k=j-1;
	while(i<=k) {
		if(i==k||k-i==1) {
			if(i==k)
				printf("%05d %d -1\n",b[i],a[c[i]].data);
			else {
				printf("%05d %d %05d\n",b[k],a[c[k]].data,b[i]);
				printf("%05d %d -1\n",b[i],a[c[i]].data);
			}
		} else {
			printf("%05d %d %05d\n",b[k],a[c[k]].data,b[i]);
			printf("%05d %d %05d\n",b[i],a[c[i]].data,b[k-1]);
		}
		i++;
		k--;
	}

	return 0;
}
在C语言中,你可以通过迭代的方式来解决这个问题。这里是一个简单的解决方案,使用三个指针,两个用来遍历原链表,第三个用来保存当前节点并将其加入到新链表的末尾: ```c #include <stdio.h> #include <stdlib.h> // 定义链表结构 typedef struct Node { int data; struct Node* next; } Node; // 创建新链表头结点 Node* createNewHead() { return (Node*)malloc(sizeof(Node)); } // 插入节点到新链表 void insertInOrder(Node** head, Node* current) { if (*head == NULL) { *head = current; } else { Node* last = *head; while (last->next != NULL) { last = last->next; } last->next = current; } } // 重新排列链表 void reverseRotate(Node** head) { if (*head == NULL || (*head)->next == NULL) { return; // 如果链表只有一个元素或者为空,无需操作 } Node* fast = *head; Node* slow = *head; // 找到中间节点 while (fast->next->next != NULL) { fast = fast->next->next; slow = slow->next; } // 从中间节点开始交换 slow->next = *head; fast->next = NULL; // 将剩余部接到新链表的尾部 insertInOrder(head, *head); } // 打印链表 void printList(Node* node) { while (node != NULL) { printf("%d ", node->data); node = node->next; } printf("\n"); } int main() { // 初始化示例链表 Node* list = createNewHead(); list->data = 1; list->next = createNewHead(); list->next->data = 2; // ...继续添加其他节点... printf("Original List:\n"); printList(list); reverseRotate(&list); printf("Reversed List:\n"); printList(list); return 0; } ``` 在这个代码里,`reverseRotate`函数首先找到链表的中间节点,然后改变连接,使得最后的节点连接到头节点,实现了题目所描述的顺序。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

摆烂.MVP

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值