这题本身不难,但是需要注意的是将最后一个数挪到前面后,倒数第2个数的next需要置为None,否则就形成了一个环无线循环。代码如下:
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def reorderList(self, head):
"""
:type head: ListNode
:rtype: void Do not return anything, modify head in-place instead.
"""
if not head:
return
if not head.next:
return
list1 = []
head1 = head
while head1:
list1.append(head1)
head1 = head1.next
b = (len(list1) - 1) / 2
head2 = head
for i in range(b):
a = list1.pop()
a.next = head2.next
head2.next = a
head2 = a.next
list1[-1].next = None