Define a function, enter the head node of a linked list, reverse the list and output the head node of the inverted list.
To reverse a single-linked list is to make the nodes in the list inverse, but not to traverse the list each time the node is recreated for the head plug, so that is to re-create a single-linked list instead of the original single-linked list to reverse, so you can think of multiple pointers to control, to prevent the next node is not found in the problem, Also to solve the link problem of the reverse chain list;
The program is as follows:
#include <iostream> #include <assert.h>using namespace std; template <class t>struct listnode//linked list node structure { t _data; listnode<t>* _next;}; template <class t>listnode<t>* buy_node (T data)//Create a linked table node { listnode<t>* tmp = new listnode<t>; tmp- >_data = data; tmp->_next = NULL; Return tmp;} template <class t>void init_list (listnode<t>** node, t data)// Initialize the linked list { *node = buy_node (data);} template <class t>void push_node (listnode<t>*& head, t DATA)//Insert list Node { if (head == null) { &nbsP; init_list (&head, data); return; } listnode<t>* tmp = head; while (tmp->_next != null) { tmp = tmp->_next; } tmp->_next = buy_node (data);} template <class t>void print_list (listnode<t>* head)//Print List { while (head != null) { cout<
To run the program:
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/80/12/wKioL1c21avgWStzAAAT_VTSEmk406.png "title=" Reverselist.png "alt=" Wkiol1c21avgwstzaaat_vtsemk406.png "/>
Finish
This article is from the "Knock Code good Sleep zzz" blog, please be sure to keep this source http://2627lounuo.blog.51cto.com/10696599/1773330
Inverted single-linked list--16