Skip to content

Latest commit

 

History

History
58 lines (43 loc) · 1.28 KB

_328. Odd Even Linked List.md

File metadata and controls

58 lines (43 loc) · 1.28 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 22, 2024

Last updated : June 22, 2024


Related Topics : Linked List

Acceptance Rate : 61.493 %


Solutions

Python

# NOTE: Requirement is O(1) space O(n) runtime

# 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: Optional[ListNode]) -> Optional[ListNode]:
        oddDummy = ListNode()
        oddDummyHead = oddDummy
        evenDummy = ListNode()

        curr = head
        indx = 0
        while curr :
            if indx % 2 == 0 :
                evenDummy.next = curr
                evenDummy = curr
            else :
                oddDummy.next = curr
                oddDummy = curr

            indx += 1
            curr = curr.next

        evenDummy.next = oddDummyHead.next
        oddDummy.next = None

        return head