思路:分别处理奇偶,保存奇偶的第一个和最后一个节点,注意最后链接的时候需要把偶数的next去掉再拼接不然就成环了
class Solution:def oddEvenList(self, head: ListNode) -> ListNode:if not head or not head.next or not head.next.next:return headfirst_odd = headfirst_even = head.nextlast_odd = headlast_even = head.nextcurrent_node = first_even.nextnode_num = 3while current_node:next_node = current_node.nextif node_num % 2 == 1:last_odd.next = current_nodelast_odd=current_nodeelse:last_even.next = current_nodelast_even=current_nodecurrent_node = next_nodenode_num += 1last_odd.next=first_evenlast_even.next=Nonereturn first_odd