最近开始用Python解题
未学过正规的Python 因此对于一些观念尚不太了解
题目是将链表中的元素两两对调
例如: Given 1->2->3->4, return the list as 2->1->4->3
我的Code如下:
class Solution(object):
def swapPairs(self, head):
if head is None or head.next is None:
return head
dummy = ListNode(0)
dummy.next = head
pre = dummy
tmp = head
while tmp and tmp.next:
pre.next = tmp.next
tmp.next = tmp.next.next <
作者: s06yji3 (阿南) 2016-08-03 19:22:00
你那两个对调,在while后的第3行就变成无限循环了物件的=是设定reference看错,是第二行就变无限循环了(对调的第一行)