Day 23 两两交换链表
2020-06-15 本文已影响0人
快乐的老周
没看懂,只能先抄了再说
class ListNode:
def init(self, x):
self.val = x
self.next = None
class Solution:
def swapPairs(self, head):
if head is None or head.next is None:
return head
tmp = head.next
r = self.swapPairs(tmp.next)
tmp.next = head
head.next = r
return tmp
if name == 'main':
a = [1,2,3,4,5]
head = ListNode(a[0])
tmp = head
for i in a[1:]:
node = ListNode(i)
tmp.next = node
tmp = tmp.next
tmp = head
while tmp:
print(tmp.val)
tmp = tmp.next
shead = Solution().swapPairs(head)
tmp = shead
while tmp:
print(tmp.val)
tmp = tmp.next