forked from JushuangQiao/Python-Offer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sixteen.py
73 lines (61 loc) · 1.53 KB
/
sixteen.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# coding=utf-8
"""
反转链表
需要考虑空链表,只有一个结点的链表
"""
def reverse_link(head):
if not head or not head.next:
return head
then = head.next
head.next = None
last = then.next
while then:
then.next = head
head = then
then = last
if then:
last = then.next
return head
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
class Nodes(object):
def __init__(self, values=None):
self.nodes = self._set_link(values) if values else None
def get_link(self):
return self.nodes
def print_self(self):
Nodes.print_link(self.nodes)
@staticmethod
def print_link(link=None):
count = 1
while link:
if count == 1:
print link.val,
elif count % 5 == 0:
print '->', link.val
else:
print '->', link.val,
count += 1
link = link.next
print
def _set_link(self, values):
head = ListNode(0)
move = head
try:
for val in values:
tmp = ListNode(val)
move.next = tmp
move = move.next
except Exception as e:
print e
return head.next
if __name__ == '__main__':
nodes = Nodes([1, 2, 3])
link = nodes.get_link()
nodes.print_self()
head = reverse_link(link)
nodes.nodes = head
Nodes.print_link(head)
nodes.print_self()