我今天在leetcode上做了一些編程問題。 我試圖解決的問題在此link。python鏈表超時時間超過
我能解決使用下面的代碼的問題:
class Solution(object):
def oddEvenList(self, head):
if not head:
return head
oddPointer = head
evenPointer = head.next
temp = head.next
while evenPointer and evenPointer.next:
oddPointer.next = evenPointer.next
oddPointer = oddPointer.next
evenPointer.next = oddPointer.next
evenPointer = evenPointer.next
oddPointer.next = temp
return head
但是如果我更改爲下面的代碼,在線法官給我一個期限超過錯誤。我想知道這裏有什麼問題。
class Solution(object):
def oddEvenList(self, head):
if not head:
return head
oddPointer = head
evenPointer = head.next
while evenPointer and evenPointer.next:
oddPointer.next = evenPointer.next
oddPointer = oddPointer.next
evenPointer.next = oddPointer.next
evenPointer = evenPointer.next
oddPointer.next = head.next #this is the change
return head
我記得我可以在Java中做到這一點,但由於某種原因,python不能這樣做。
感謝您的回答!我有一個後續問題:在這種情況下,head.next是否還有值? –