Nine degrees _ topic 1518: Reverse linked list

Source: Internet
Author: User

The back-interpolation method establishes the linked list, and then starts with the second element (if any) to insert after the end node to achieve the reverse list effect.

Topic Description: Enter a list, reverse the linked list, output linked list of all elements.
(Hint: Make sure to use the linked list)
Input:
The input may contain more than one test sample, and the input ends with EOF.
For each test case, enter the first behavior of an integer n (0<=n<=1000): Represents the number of linked lists that will be entered.
The second line entered contains n integers t (0<=t<=1000000): Represents a list element.
Output:
corresponding to each test case,
The element that is reversed with this output list, or null if there are no elements.
Sample input:
5
1 2 3 4 5
0
Sample output:
5 4 3 2 1
Null

#include <iostream> #include <cstdlib> using namespace std;
    typedef struct LNODE {struct Lnode *next;
int data;
}*linklist;
    Linklist createlist (linklist list,int n) {int num=0;
    Linklist P=null,q=null;
    p= (linklist) malloc (sizeof (linklist *));
    list=p;
    p->next=null;
        for (int i=0;i<n;i++) {q= (linklist) malloc (sizeof (linklist *));
        cin>>num;
        q->data=num;
        q->next=null;
        p->next=q;
 
    p=q;
} return list;
    } linklist desendlist (linklist list) {linklist p=list->next,q=p->next,s=null;
    p->next=null;
        while (q)//q points to the element {s=q that needs to be inserted after the header node;
        q=q->next;
        s->next=p;
        list->next=s;
    P=s;
} return list;
    } void Traveselist (linklist list) {linklist p=list->next;
        if (p) {cout<<p->data;
    p=p->next;
    else {cout<< "NULL" <<endl;return;}
   while (p) {cout<< ' ' <<p->data;
    p=p->next;
} cout<<endl;
    int main () {int n;
        while (cin>>n) {if (!n) {cout<< "NULL" <<endl;continue;}
        Linklist List=null;
        List=createlist (list, n);
        List=desendlist (list);
    Traveselist (list);
return 0;
    }/************************************************************** problem:1518 USER:HNDXZTF language:c++ result:accepted time:250 Ms memory:2972 KB ****************************************************************/

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.