2019-07-27 |

链表成对调换

A
B
C
D
答案:

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

解释:
剑指offer

发表评论

    评价:
    验证码: 点击我更换图片
    最新评论