听说这是 O(1) 空间复杂度 O(n) 时间复杂度 ?有大神解释下下面这个算吗?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

struct MyLink {
MyLink* next;
int nVal;
};


MyLink* genLink(MyLink* next,int nval){
MyLink* tmp = new MyLink;
tmp->nVal = nval;
tmp->next = next;
return tmp;
}


MyLink* reverseLink(MyLink* begin){
MyLink* tmp = nullptr;
while (begin){
auto top = begin->next;
begin->next = tmp;
tmp = begin;
begin = top;
}
return tmp;
}

void printLink(MyLink* begin,const char* prfix){
while (begin){
printf("%s val:%d\n", prfix, begin->nVal);
begin = begin->next;
}
}

int _tmain(int argc, _TCHAR* argv[])
{
MyLink*head = nullptr;
for (auto i = 0; i < 5; i++){
head = genLink(head, i);
}
printLink(head,"orglink ");
head = reverseLink(head);
printLink(head,"reversed link ");

while (head){
auto tmp = head->next;
delete head;
head = tmp;
}
return 0;
}
感谢您的阅读,本文由 smallwhite's Blog 版权所有。如若转载,请注明出处:smallwhite's Blog(https://smallwhite.ml/pub/uncategorized/yi-ge-while-shi-xian-c-fan-zhuan-dan-xiang-lian-biao.html
redis lua and nodejs
cocos ios,other linker flags 加入 -objc 编译报错解决方案