1. 程式人生 > >leetcode 奇偶連結串列 python

leetcode 奇偶連結串列 python

1 # Definition for singly-linked list. 2 # class ListNode: 3 # def __init__(self, x): 4 # self.val = x 5 # self.next = None 6 7 class Solution: 8 def oddEvenList(self, head): 9 """ 10 :type head: ListNode 11 :rtype: ListNode 12 """ 13 tail = head
14 cur = head 15 while tail.next is not None: 16 tail = tail.next 17 mid = tail 18 while cur.val != mid.val or mid.next is not None: 19 odd = cur.next 20 cur.next = odd.next 21 odd.next = None 22 cur = cur.next
23 tail.next = odd 24 tail = tail.next 25 return head