← 返回题库
中级

两两交换链表中的节点

未完成
中级参考 代码结构已给出,请填写 ____ 处
class ListNode:
    def __init__(____):
        self.val = val
        self.next = next

def solve():
    def swapPairs(____):
        if not head or not head.next:
            return head
        first = head
        second = head.next
        first.next = swapPairs(____)
        second.next = first
        return second
    head = ListNode(____, ListNode(____, ListNode(____, ListNode(____))))
    result = swapPairs(____)
    vals = []
    while result:
        vals.append(____)
        result = result.next
    print(____)

示例

输入
solve()
期望输出
[2, 1, 4, 3]
Python 代码 🔒 登录后使用
🔒

登录后即可练习

注册免费账号,在浏览器中直接运行 Python 代码