Skip to content

Commit 3659716

Browse files
committed
内容错误,重新提交
1 parent 6674f04 commit 3659716

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

reorder_list.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,45 @@ class Solution:
55
@param head: The first node of the linked list.
66
@return: nothing
77
"""
8+
def reorderList(self, head):
9+
# write your code here
810
def reorderList(self, head):
911
# write your code here
1012
'''
1113
0->1->2->3->4->5 (n = 5) => 0->5->1->4->2->3 (012/543)
1214
0->1->2->3->4->5->6 (n = 6) => 0->6->1->5->2->4->3 (0123/654)
1315
'''
1416
if head:
15-
slow, fast = head, head # fast每次前进2步
17+
prev, slow, fast = None, head, head # fast每次前进2步
1618
while fast:
19+
prev = slow
20+
slow = slow.next
1721
fast = fast.next
1822
if not fast:
1923
break
2024
fast = fast.next
21-
slow = slow.next
22-
print slow.val, fast.val
23-
25+
old_tail = prev
26+
old_tail.next = None
27+
new_head = slow
28+
new_head = self.reverse(new_head)
29+
while head:
30+
if new_head:
31+
_next = head.next
32+
head.next = new_head
33+
new_head = new_head.next
34+
head.next.next = _next
35+
head = _next
36+
else:
37+
head = head.next
38+
39+
def reverse(self, head):
40+
# write your code here
41+
if not head:
42+
return None
43+
new_head = None
44+
while head:
45+
node = head
46+
head = head.next
47+
node.next = new_head
48+
new_head = node
49+
return new_head

0 commit comments

Comments
 (0)