【发布时间】:2020-03-15 00:37:40
【问题描述】:
我正在尝试在 leetcode (https://leetcode.com/problems/swap-nodes-in-pairs/) 上“成对交换节点”。
我的代码如下。它仅对 1 个测试用例产生 TLE 错误。我真的不确定它是否陷入无限循环。感谢任何帮助。
def swapPairs(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
ans=h=ListNode(0)
while head and head.next:
h.next=head.next
h.next.next=head
h=h.next.next
head=head.next.next
h.next=head
return ans.next
【问题讨论】:
-
嗨@Tommy。请查看我的解决方案,如果它有帮助以及您有任何问题,请告诉我。谢谢。
标签: python linked-list