Question: https://oj.leetcode.com/problems/reorder-list/
Question Name: Reorder List
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 | # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: # @param head, a ListNode # @return nothing def reorderList(self, head): listInArray = [] # Store all the nodes in the list into an array. current = head while current != None: listInArray.append(current) current = current.next # Check whether it's an empty list. listLen = len(listInArray) if listLen == 0: return # Reorder the List as required for index in xrange(listLen // 2): listInArray[index].next = listInArray[listLen-1-index] listInArray[listLen-1-index].next = listInArray[index+1] # End the list listInArray[listLen // 2].next = None return |