I think this is even cleaner: def oddEvenList(self, head: Optional[ListNode]) -> Optional[ListNode]: if not head or not head.next or not head.next.next: return head odd = head even = head.next even_head = even while even and even.next: odd.next = odd.next.next even.next = even.next.next odd = odd.next even = even.next odd.next = even_head return head
class Solution: def oddEvenList(self, head: Optional[ListNode]) -> Optional[ListNode]: if not head or not head.next: return head oddptr = head evenptr = evenhead = head.next i = 1 while evenptr and evenptr.next: oddptr.next = evenptr.next oddptr = oddptr.next evenptr.next = oddptr.next evenptr = evenptr.next i += 2 oddptr.next = evenhead return head