Topic 1518: Reverse linked list

Source: Internet
Author: User
Tags printf
 Title Description:

Enter a list of all the elements of the linked list after the list is reversed.
(Hint: Be sure to use a linked list)

Input:

The input may contain multiple test samples, and the input ends with EOF.
For each test case, the first behavior of the input is an integer n (0<=n<=1000): Represents the number of linked lists that will be entered.
The second line of input contains n integers t (0<=t<=1000000): Represents the linked list element.

Output:

corresponding to each test case,
This outputs the inverted element of the list, and outputs null if there are no elements.

Sample Input:

5
1 2 3 4 5
0
Sample output:
5 4 3 2 1
NULL
#include <stdio.h> #include <malloc.h> struct node {int data;
struct node *next;
};
    void Main () {int n,i; while (scanf ("%d", &n)!=eof) {struct node *head,*p; Head=null; Do not allocate memory to head         for (i=0;i<n;i++)         { 
           int k;
            p= (struct node *) malloc (sizeof (struct node));
            scanf ("%d", &p->data);
            p->next=head;
            head=p;        }         for (;p!=null;p=p->next )         {            if (p->next==null)             {   
             printf ("%d\n", p->data);            }              Else                
printf ("%d", p->data);        }         if (p==head)          {            printf ("null\
n ");

        }    }}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.