首页 > 代码库 > Leetcode #24. Swap nodes in pairs

Leetcode #24. Swap nodes in pairs

1. 使用dummy node保存表头

2. cur -> head -> head.next -> p

 1 class Solution(object):
 2     def swapPairs(self, head):
 3         """
 4         :type head: ListNode
 5         :rtype: ListNode
 6         """
 7         dummy = cur = ListNode(0)
 8         dummy.next = head
 9 
10         while head != None and head.next != None:
11             p = head.next.next
12             cur.next = head.next
13             cur.next.next = head
14             head.next = p
15             cur = head
16             head = p
17 
18         return dummy.next

 

Leetcode #24. Swap nodes in pairs