遍历两次单项链表

不使用数组作为缓存排序的方式,

class Solution:
    def oddEvenList(self, head: ListNode):
        h = head
        res = ListNode(0)
        p = res
        i = 1
        while head is not None:
            if i % 2 != 0:
                # print(head.val)
                p.next = ListNode(head.val)
                p = p.next
            head = head.next
            i += 1
        head = h
        while head is not None:
            if i % 2 == 0:
                # print(head.val)
                p.next = ListNode(head.val)
                p = p.next
            head = head.next
            i += 1

        return res.next

alt