# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def oddEvenList(self, head: ListNode) -> ListNode:
if head==None or head.next==None or head.next.next==None:
return head
p=head.next #偶数位
q=head.next.next #奇数位
head.next=q
tmp=p
while q.next: #q.next为偶数位,说明还有偶数位
p.next=p.next.next
p=p.next
q.next=q.next.next
if q.next:
q=q.next #while循环结束后q.next=None
q.next=tmp #奇数位连接到偶数位上
p.next=None #偶数位最后一位连接None
return head