Q: implement a function that give a list head as a arg, and return another list head which the list reversed.
PS: 对于我这个算法的实现,还算比较满意。最近在阅读stl源码里面,找到了类似的实现,大家做了比较
stl_slist.h
inline __slist_node_base* __slist_reverse(__slist_node_base* node)
{
__slist_node_base* result = node;
node = node->next;
result->next = 0;
while(node) {
__slist_node_base* next = node->next;
node->next = result;
result = node;
node = next;
}
return result;
}
基本差不多。