标签:code image 创建 tco src 超时 http com head
题目描述:
方法一:创建新节点(超时)
class Solution: def swapPairs(self, head: ListNode) -> ListNode: dummy=ListNode(0) p = dummy h = head while h: if h and h.next: p.next = ListNode(h.next.val) p = p.next p.next = ListNode(h.val) h = h.next.next else: p.next = ListNode(h.val) p = p.next return dummy.next
方法二;
class Solution: def swapPairs(self, head: ListNode) -> ListNode: dummy=ListNode(0) p = dummy h = head while h: if h and h.next: tmp = h.next p.next = tmp h.next = h.next.next tmp.next = h h = h.next p = p.next.next else: p.next = h h = h.next return dummy.next
方法三:递归
class Solution: def swapPairs(self, head: ListNode) -> ListNode: if not head or not head.next: return head tmp = head.next head.next = self.swapPairs(head.next.next) tmp.next = head return tmp
标签:code image 创建 tco src 超时 http com head
原文地址:https://www.cnblogs.com/oldby/p/11163612.html