Subject description:
method one:
class Solution(object): def reorderList(self, head): """ :type head: ListNode :rtype: None Do not return anything, modify head in-place instead. """ p = head l = [] while p: l.append(p) p = p.next n = len(l) for i in range(n//2): l[i].next = l[n-i-1] l[n-i-1].next = l[i+1] if n > 0: l[n//2].next = None
Method Two:
class Solution(object): def reorderList(self, head): """ :type head: ListNode :rtype: None Do not return anything, modify head in-place instead. """ if not head: return head dummy = ListNode(0) dummy.next = head fast,slow = head,head while fast and fast.next: fast = fast.next.next slow = slow.next cur = slow.next slow.next = None p = None while cur: next_cur = cur.next cur.next = p p = cur cur = next_cur pre = head while p: tem = pre.next pre.next = p tem2 = p.next p.next = tem pre = tem p = tem2 return dummy.next