单链表就地逆置
谁帮我写个简单的单链表就地逆置的代码啊。求求求求!!!!急用啊!!感激不尽啊1
其实只要把元素再从新插入一边就行了 头插法
比如原来是 1 2 3 4 ... n
第1个元素头插 1 2 3 4 ... n
第2个元素头插 2 1 3 4 ... n
...
第n个元素头插 n n-1 ... 1

程序代码:
#include <string>
#include <iostream>
#include <list>
using namespace std;
template<typename T>
void display(const list<T>& _list)
{
list<T>::const_iterator iter = _list.begin();
while(iter != _list.end())
{
cout << *iter << " ";
++iter;
}
cout << endl;
}
int main(int argc, char *argv[])
{
list<string> strList;
string strInput;
while(cin >> strInput)
{
strList.push_back(strInput);
strInput.clear();
}
display(strList);
strList.reverse();
display(strList);
return 0;
}
