链表_leetcode24

# Definition for singly-linked list.
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None

class Solution(object):
def swapPairs(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""

dummyHead = ListNode(0)
dummyHead.next = head
p = dummyHead


while p.next and p.next.next:

node1 = p.next
node2 = node1.next
next = node2.next

p.next = node2
node2.next = node1
node1.next = next

p = node1

return dummyHead.next
posted @ 2019-03-19 10:48  AceKo  阅读(97)  评论(0编辑  收藏  举报