// 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