Skip to content

Commit

Permalink
Time: 42 ms (86.39%), Space: 18.9 MB (68.78%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
hovanhoa committed Oct 18, 2023
1 parent daec7f7 commit e2b5475
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions 0328-odd-even-linked-list/0328-odd-even-linked-list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 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]:
oddHead = odd = ListNode(0)
evenHead = even = ListNode(0)
isOdd = True

while head:
if isOdd:
odd.next = head
odd = head
else:
even.next = head
even = head
head = head.next
isOdd = not isOdd

even.next = None
odd.next = evenHead.next

return oddHead.next

0 comments on commit e2b5475

Please sign in to comment.