面试 居然问如何 反转单向链表~~

面试当时有点紧张,犯2了,居然写了个空间O(N)的垃圾反转代码。其实很简单,刚刚用两三个指针折腾下就行了,代码如下 :

ZList * ListAlgorithm::ListConverse(ZList *L)

{

ZList *Pre= NULL;

ZList *Cur = L;

ZList *Next = NULL;

while(Cur != NULL)

{

Next = Cur->next;

Cur->next = Pre;

Pre = Cur;

Cur = Next;

}

return Pre;

}

posted @ 2011-08-28 14:13  Jason Cai  阅读(898)  评论(0编辑  收藏  举报