def reorderList(self, head): """ :type head: ListNode :rtype: void Do not return anything, modify head in-place instead. """ t = head a = [] while t: a.append(t) t = t.next if len(a) < 3: return # print(a[1].next.val) for i in range(0, len(a) // 2): tmp = a[i].next a[i].next = a[~i] if tmp == a[~i]: a[~i].next = None else: a[~i].next = tmp tmp.next = None test = False if test: from ListNode.ListNode import ListNode ll = ListNode.make([1, 2, 3, 4, 5]) s = Solution() print(s.reorderList(ll)) print(ll)
class Solution: def insertionSortList(self, head): """ :type head: ListNode :rtype: ListNode """ if head is None: return head out = [] while head: out.append(head) head = head.next # print(out[-1].val) out = sorted(out, key=lambda x: x.val) # print([x.val for x in out]) for i in range(0, len(out) - 1): out[i].next = out[i + 1] out[len(out) - 1].next = None return out[0] test = False if test: from ListNode.ListNode import ListNode ll = ListNode.make([5, 1, 2, 3, 4]) s = Solution() print(s.insertionSortList(ll))