def oddEvenList2(self, head):
        # 48ms
        if head is None:
            return None
        count = 0
        list_odd, list_even = [], []
        while head is not None:
            if count % 2 == 0:
                list_even.append(head.val)
            else:
                list_odd.append(head.val)
            count += 1
            head = head.next

        if len(list_even) > 0:
            res = ListNode(list_even[0])
            res_head = res
            for i in range(1, len(list_even)):
                res.next = ListNode(list_even[i])
                res = res.next

            if len(list_odd) > 0:
                res.next = ListNode(list_odd[0])
                res = res.next
                for i in range(1, len(list_odd)):
                    res.next = ListNode(list_odd[i])
                    res = res.next

        return res_head
예제 #2
0
    def rotateRight(self, head, k):
        # 24ms
        if head is None or head.next is None:
            return head

        dummy = ListNode(0)
        dummy.next = head
        fast, slow = dummy, dummy

        i = 0
        while fast.next is not None:
            fast = fast.next
            i += 1

        j = i - k % i
        while j > 0:
            slow = slow.next
            j -= 1

        fast.next = dummy.next
        dummy.next = slow.next
        slow.next = None

        return dummy.next