zoukankan      html  css  js  c++  java
  • 九度oj题目1518:反转链表

    题目1518:反转链表

    时间限制:1 秒

    内存限制:128 兆

    特殊判题:

    提交:2567

    解决:948

    题目描述:

    输入一个链表,反转链表后,输出链表的所有元素。
    (hint : 请务必使用链表)

    输入:

    输入可能包含多个测试样例,输入以EOF结束。
    对于每个测试案例,输入的第一行为一个整数n(0<=n<=1000):代表将要输入的链表的个数。
    输入的第二行包含n个整数t(0<=t<=1000000):代表链表元素。

    输出:

    对应每个测试案例,
    以此输出链表反转后的元素,如没有元素则输出NULL。

    样例输入:
    5
    1 2 3 4 5
    0
    样例输出:
    5 4 3 2 1
    NULL
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <string>
     4 #include <queue>
     5 #include <stack>
     6 #include <iostream>
     7 using namespace std;
     8 struct node{
     9     node *fro,*next;
    10     int v;
    11 };
    12 int main(){
    13     //freopen("D:\INPUT.txt","r",stdin);
    14     node *head,*tail;
    15     int n;
    16     while(scanf("%d",&n)!=EOF){
    17         if(!n){
    18             cout<<"NULL"<<endl;
    19         }
    20         else{
    21             head=new node();
    22             tail=head;
    23             head->fro=head->next=NULL;
    24             scanf("%d",&head->v);
    25             int i;
    26             node *p;
    27             for(i=1;i<n;i++){//输入
    28                 p=new node();
    29                 p->fro=tail;
    30                 tail->next=p;
    31                 scanf("%d",&p->v);
    32                 p->next=NULL;
    33                 tail=p;
    34             }
    35             cout<<tail->v;
    36             p=tail->fro;
    37             delete tail;
    38             p->next=NULL;
    39             tail=p;
    40             while(tail!=head){//转置链表
    41                 cout<<" "<<tail->v;
    42                 p=tail->fro;
    43                 delete tail;
    44                 tail=p;
    45             }
    46             cout<<" "<<tail->v<<endl;
    47             delete head;
    48         }
    49     }
    50     return 0;
    51 }
  • 相关阅读:
    SQL server使用
    NCC 事务
    springboot学习
    容器
    x86汇编
    git之.gitignore文件用途
    Linux系统安装之U盘引导
    使用异步I/O大大提高应用程序的性能
    Python3.5 用 pip 安装lxml时出现 “Unable to find vcvarsall.bat ”?(转载)
    python之正则表达式
  • 原文地址:https://www.cnblogs.com/Deribs4/p/4649467.html
Copyright © 2011-2022 走看看