美文网首页
34.链表成对调换

34.链表成对调换

作者: vbuer | 来源:发表于2018-09-04 09:34 被阅读16次

    1->2->3->4转换成2->1->4->3.

    class ListNode:
        def __init__(self, x):
            self.val = x
            self.next = None
    
    class Solution:
        # @param a ListNode
        # @return a ListNode
        def swapPairs(self, head):
            if head != None and head.next != None:
                next = head.next
                head.next = self.swapPairs(next.next)
                next.next = head
                return next
            return head
    

    相关文章

      网友评论

          本文标题:34.链表成对调换

          本文链接:https://www.haomeiwen.com/subject/nfzniftx.html