数据结构算法操作试题(C++/Python):数据结构算法操作试题(C++/Python)——目录
leetcode 链接:https://leetcode-cn.com/problems/swap-nodes-in-pairs/submissions/
python:24ms,10.7MB, 99.03%
class Solution(object):
def swapPairs(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
tmp_head = pre_head = ListNode(0)
pre_head.next = head
while tmp_head.next and tmp_head.next.next:
next2_p = tmp_head.next.next.next
tmp_head.next.next.next = tmp_head.next
tmp_head.next = tmp_head.next.next
tmp_head.next.next.next = next2_p
tmp_head = tmp_head.next.next
return pre_head.next
其他方法看 leetcode 链接 评论区~