题目:给出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; } }
时间: 2024-11-06 09:30:55