-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path86.py
More file actions
98 lines (95 loc) · 2.03 KB
/
86.py
File metadata and controls
98 lines (95 loc) · 2.03 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# Definition for singly-linked list.
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
class Solution(object):
def partition(self, head, x):
"""
:type head: ListNode
:type x: int
:rtype: ListNode
"""
new_head = None
new_tail = None
old_head = None
old_tail = None
if not head:
return head
while head:
if head.val >= x:
if not old_head:
old_head = head
old_tail = head
else:
old_tail.next = head
old_tail = head
else:
if not new_head:
new_head = head
new_tail = head
else:
new_tail.next = head
new_tail = head
head = head.next
if old_tail:
old_tail.next = None
if new_tail:
new_tail.next = old_head
return new_head
else:
return old_head
import time
def test1():
flag = None
for i in range(1000000):
if not flag:
a = 1
def test2():
flag = None
for i in range(1000000):
if flag is not None:
a = 1
def test3():
flag = 3
for i in range(1000000):
if flag >= 3:
a = 1
def test4():
flag = 3
for i in range(1000000):
if flag < 4:
a = 1
t1 = time.time()
test1()
t2 = time.time()
print 'time for test1', t2 - t1
t1 = time.time()
test2()
t2 = time.time()
print 'time for test2', t2 - t1
t1 = time.time()
test3()
t2 = time.time()
print 'time for test3', t2 - t1
t1 = time.time()
test4()
t2 = time.time()
print 'time for test4', t2 - t1
solu = Solution()
a = ListNode(1)
b = ListNode(4)
c = ListNode(3)
d = ListNode(2)
e = ListNode(5)
f = ListNode(2)
a.next = b
b.next = c
c.next = d
d.next = e
e.next = f
res = solu.partition(a, 3)
print res
while res:
print res.val,
res = res.next