Skip to content

Latest commit

 

History

History
38 lines (28 loc) · 717 Bytes

328.md

File metadata and controls

38 lines (28 loc) · 717 Bytes

328 Odd Even Linked List

Description

link


Solution

See Code


Code

O(n) - O(1)

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def oddEvenList(self, head: 'ListNode') -> 'ListNode':
        dummy1 = odd = ListNode(0)
        dummy2 = even = ListNode(0)
        while head:
            odd.next = head
            even.next = head.next
            odd = odd.next
            even = even.next
            head = head.next.next if even else None
        odd.next = dummy2.next
        return dummy1.next