算法导论 10.2-7

题目:给出O(N)运行时间的非递归算法,实现对一个含N个元素的链表的逆转,要求空间复杂度为O(1)

代码:

/* Assuming List with header and L is not empty */

List ReverseList( List L )
{
    Position CurrentPos, NextPos;

    NextPos = L->Next;
    L->Next = NULL;

    while ( NextPos != NULL )
    {
        CurrentPos = NextPos;
        NextPos = NextPos->Next;
        CurrentPos->Next = L->Next;
        L->Next = CurrentPos;
    }
}

 

posted @ 2014-10-18 13:57  Leptus  阅读(225)  评论(0编辑  收藏  举报