题目描述
按数字输入顺序创建单链表。不可借助数组、容器,不可开辟新结点空间。编写函数,在原链表上实现单链表的反转。例如单链表10->20->30,反转后变为单链表30->20->10。
注:不符合题目要求,使用上述逆序输出不计分。
输入
测试次数t
每组测试数据一行,格式如下:
数据个数n,后跟n个整数
输出
对每组测试数据,输出反转后的单链表。
输出样例:
2
10 1 2 3 4 5 6 7 8 9 10
4 19 20 15 -10
输出样例:
10 9 8 7 6 5 4 3 2 1
-10 15 20 19
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
struct Node{
int value;
Node *next;
};
int main()
{
int t;
cin>>t;
while(t--){
Node *head=new Node{0,NULL};
int n;
cin>>n;
for(int i=0;i<n;i++){
int value;
cin>>value;
Node *p=new Node{value, NULL};
p->next=head->next;
head->next=p;
head->value++;
}
Node *p=head->next;
while(p->next){
cout<<p->value<<" ";
p=p->next;
}
cout<<p->value<<" "<<endl;
}
}