Untitled

mail@pastecode.io avatar
unknown
python
2 years ago
776 B
1
Indexable
Never
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def oddEvenList(self, head: ListNode) -> ListNode:
        isOdd = True
        cur = head
        dummyOddHead = ListNode(-1)
        oddCur = dummyOddHead
        dummyEvenHead = ListNode(-1)
        evenCur = dummyEvenHead
        while cur:
            if isOdd:
                oddCur.next = cur
                oddCur = oddCur.next
            else:
                evenCur.next= cur
                evenCur = evenCur.next

            isOdd = False if isOdd else True
            cur = cur.next

        oddCur.next = dummyEvenHead.next
        evenCur.next = None

        return dummyOddHead.next