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
Recommendation index :※※
Source: http://ac.jobdu.com/problem.php? PID = 1, 1518
This topic is the operation of the linked list. In OJ, the structure of the linked list is required (otherwise it will be too ...).
To flip the linked list, you need to adjust the pointer direction, namely, tail, Head, and tail.
1. The direct idea is to scan the linked list and use a pointer to keep the pointer intact when you flip each pointer. (Supplement tomorrow)
# Include <iostream> # include <stdio. h> # include <stdlib. h> using namespace STD; typedef struct node {int val; node * Next;} node; int main () {int N; while (scanf ("% d ", & N )! = EOF) {node * head = new node; head-> next = NULL; node * pre = head; int I; for (I = 0; I <N; I ++) {node * No = new node; scanf ("% d", & NO-> Val); no-> next = NULL; pre-> next = no; pre = no;} If (Head-> next = NULL) printf ("null \ n"); else {node * P = head-> next; node * pre = NULL; while (P! = NULL) {node * q = p-> next; P-> next = pre; Pre = P; P = Q;} head-> next = pre; P = head-> next; If (P! = NULL) printf ("% d", p-> Val); While (p-> next! = NULL) {P = p-> next; printf ("% d", p-> Val);} printf ("\ n") ;}} return 0 ;}
2. Create a New nhead, scanner link, and add each read node to nhead-> next .. In this way, after reading the original linked list, it is flipped. (If you do not want to break the structure of the source linked list, you can use the copy node method only when reading the source linked list .)
# Include <iostream> # include <stdio. h> # include <stdlib. h> using namespace STD; typedef struct node {int val; node * Next;} node; int main () {int N; while (scanf ("% d ", & N )! = EOF) {node * head = new node; head-> next = NULL; node * pre = head; int I; for (I = 0; I <N; I ++) {node * No = new node; scanf ("% d", & NO-> Val); no-> next = NULL; pre-> next = no; pre = no;} If (Head-> next = NULL) printf ("null \ n"); else {node * nhead = new node; nhead-> next = NULL; node * P = head-> next; while (P! = NULL) {node * q = nhead-> next; nhead-> next = P; P = p-> next; nhead-> next = Q ;} head-> next = nhead-> next; P = head-> next; If (P! = NULL) printf ("% d", p-> Val); While (p-> next! = NULL) {P = p-> next; printf ("% d", p-> Val);} printf ("\ n") ;}} return 0 ;}