Odd Even Linked List

 

328. Odd Even Linked List

Solution

  1. Use two pointer to keep track of odd and even nodes.

  2. Note to deal with head.next.next when head.next is None

    def oddEvenList(self, head):
     """
     :type head: ListNode
     :rtype: 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