Jiudu_topic 1518: reverse linked list

Source: Internet
Author: User

// Create a linked list using the backend insertion method, and insert the second element (if any) to the end of the header node in sequence to reverse the linked list.

Description:

Enter a linked list. After the linked list is reversed, all elements of the linked list are output.
(Hint: please be sure to use the Linked List)
Input:
The input may contain multiple test examples. The input ends with EOF.
For each test case, the first input behavior is an integer N (0 <= n <= 1000): represents the number of linked lists to be input.
The second line of the input contains N integers t (0 <= T <= 1000000): representing the linked list element.
Output:
Corresponding to each test case,
Output the elements after the chain table is reversed. If no element exists, null is output.
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 dese Ndlist (linklist list) {linklist P = List-> next, q = p-> next, S = NULL; P-> next = NULL; while (q) // Q points to the element {S = Q; 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 ************************************** **************************/

Jiudu_topic 1518: reverse linked list

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.