forked from zhanghe06/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list.py
More file actions
58 lines (46 loc) · 992 Bytes
/
linked_list.py
File metadata and controls
58 lines (46 loc) · 992 Bytes
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
51
52
53
54
55
56
57
58
#!/usr/bin/env python
# encoding: utf-8
"""
@author: zhanghe
@software: PyCharm
@file: ss.py
@time: 2017/6/19 下午11:51
@title: 链表
"""
# 生成器都是Iterator对象
# 方式一
# 首先获得Iterator对象:
it = iter([1, 2, 3, 4, 5]) # 列表转生成器
# 循环:
while True:
try:
# 获得下一个值:
x = next(it)
print x
except StopIteration:
# 遇到StopIteration就退出循环
break
# 方式二
l = [1, 2, 3, 4, 5]
a = (i for i in l) # 列表转生成器
print next(a) # next 是生成器的方法(需要考虑StopIteration异常情况)
def linked_list_invert(ol):
"""
链表反转
:param ol:
:return:
"""
l = []
for i in ol:
l.insert(0, i)
nl = (i for i in l)
return nl
if __name__ == '__main__':
l = [1, 2, 3, 4, 5]
a = (i for i in l)
s = linked_list_invert(a)
print s.next()
print s.next()
print s.next()
print s.next()
print s.next()