328. Odd Even Linked List

weixin_33728268發表於2018-06-20

就是兩個指標,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
        

相關文章