# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param head ListNode类
# @return ListNode类
#
class Solution:
def oddEvenList(self , head: ListNode) -> ListNode:
# write code here
dummy_odd, dummy_even = ListNode(-1), ListNode(-1)#奇头节点,偶头节点
odd, even = dummy_odd, dummy_even#奇当前节点,偶当前节点
cur, n = head, 1#大连表当前节点,当前节点编号
while cur:
if n%2:#奇数位节点
odd.next, odd, cur = cur, cur, cur.next
else:#偶数位节点
even.next, even, cur = cur, cur, cur.next
n += 1#节点位数加一
odd.next, even.next = dummy_even.next, None#奇偶位数链表进行拼接
return dummy_odd.next#返回拼接后的链表