Solution
-
Use two pointer to keep track of odd and even nodes.
-
Note to deal with
head.next.next
whenhead.next
is Nonedef 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
PREVIOUSMin Cost to Connect All Points
NEXTReverse String