就是两个指针,odd 和even的指针过程
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def oddEvenList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if not head or not head.next or not head.next.next:
return head
odd = head
even = head.next
dummy = head.next
while odd.next and even.next:
odd.next = even.next
odd = odd.next
even.next = odd.next
even = even.next
if even:
even.next = None
odd.next = dummy
return head