Classic question, the Code is as follows:
1 # include <iostream> 2 # include <vector> 3 using namespace STD; 4 struct listnode {5 Int m_idata; 6 listnode * m_pnext; 7 }; 8 listnode * reverse (listnode * in_plistnode) {9 If (in_plistnode = NULL) {10 return NULL; 11} else if (in_plistnode-> m_pnext = NULL) {12 Return in_plistnode; 13} 14 listnode * t_pfirst = in_plistnode-> m_pnext; 15 listnode * t_1_cond = in_plistnode-> m_pnext; 16 in_plistno De-> m_pnext = NULL; 17 while (t_pfirst! = NULL & t_prop cond! = NULL) {18 t_pfirst-> m_pnext = upper; 19 lower = t_pfirst; 20 t_pfirst = t_primary cond; 21 t_primary cond = t_primary cond-> m_pnext; 22} 23 t_pfirst-> m_pnext = lower; 24 return t_pfirst; 25} 26 27 int main () {28 listnode * t_ilistnode = new listnode (); 29 listnode * t_ilistnodehead = t_ilistnode; 30 for (INT I = 1; I <10; I ++) {31 t_ilistnode-> m_idata = I; 32 listnode * t_ilistnodenext = new listnode (); 33 t_ilistnode-> m_pnext = t_ilistnodenext; 34 t_ilistnode = t_ilistnodenext; 35} 36 t_ilistnode-> m_idata = 10; 37 listnode * t_itemplistnode = t_ilistnodehead; 38 cout <"before the chain table is flipped:"; 39 while (t_itemplistnode! = NULL) {40 cout <t_itemplistnode-> m_idata <"; 41 t_itemplistnode = t_itemplistnode-> m_pnext; 42} 43 cout <Endl; 44 45 t_itemplistnode = reverse (t_ilistnodehead); 46 47 cout <"after the chain table is flipped:"; 48 While (t_itemplistnode! = NULL) {49 cout <t_itemplistnode-> m_idata <"; 50 t_itemplistnode = t_itemplistnode-> m_pnext; 51} 52 cout <Endl; 53 system ("pause"); 54 return 0; 55 56 57}
Given the head pointer of a linked list, it is required to traverse only once and reverse the element order in the single-linked list.