Odd Even Linked List

Problem Id: 328 Difficulty: Medium


Intuition

Solution


class Solution:
    def oddEvenList(self, head: ListNode) -> ListNode:
        first = ListNode(None)
        f_i = first
        second = ListNode(None)
        s_i = second

        index = head
        count = 0
        while index:
            if count % 2 == 0:
                f_i.next = index
                f_i = index
            elif count % 2 == 1:
                s_i.next = index
                s_i = index
            index = index.next
            count += 1
        f_i.next = second.next
        s_i.next = None
        return first.next