最傻的方法:

    ListNode *swapPairs(ListNode *head) {
        if (head == NULL)
            return NULL;
        ListNode *temp = );
        ListNode *head_2 = temp;
        while (head != NULL && head->next != NULL) {
            temp = temp->next = new ListNode(head->next->val);
            temp = temp->next = new ListNode(head->val);
            head = head->next->next;
        }
        if (head != NULL)
            temp->next = head;
        return head_2->next;
    }

好一点的方法

    ListNode* swapPairs(ListNode* head) {
        if(!head || !head->next)
            return head;
        ListNode * temp = head->next;
        head->next = swapPairs(temp->next);
        temp->next = head;
        return temp;
    }

Leetcode--Swap Nodes in Pairs的更多相关文章

  1. LeetCode: Swap Nodes in Pairs 解题报告

    Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...

  2. [LeetCode]Swap Nodes in Pairs题解

    Swap Nodes in Pairs: Given a linked list, swap every two adjacent nodes and return its head. For exa ...

  3. [LeetCode] Swap Nodes in Pairs 成对交换节点

    Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...

  4. leetcode—Swap Nodes in Pairs

    1.题目描述 Given a linked list, swap every two adjacent nodes and return its head.   For example, Given ...

  5. Leetcode:Swap Nodes in Pairs 单链表相邻两节点逆置

    Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...

  6. [LeetCode]Swap Nodes in Pairs 成对交换

    Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...

  7. [Leetcode] Swap nodes in pairs 成对交换结点

    Given a linked list, swap every two adjacent nodes and return its head. For example,Given1->2-> ...

  8. LeetCode Swap Nodes in Pairs 交换结点对(单链表)

    题意:给一个单链表,将其每两个结点交换,只改尾指针,不改元素值. 思路:迭代法和递归法都容易写,就写个递归的了. 4ms /** * Definition for singly-linked list ...

  9. leetcode Swap Nodes in Pairs python

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

  10. 【LeetCode】Swap Nodes in Pairs 链表指针的应用

    题目:swap nodes in pairs <span style="font-size:18px;">/** * LeetCode Swap Nodes in Pa ...

随机推荐

  1. REST WCF Service中的WebMessageBodyStyle

    这个参数是个枚举包括如下值: WebMessageBodyStyle.Bare WebMessageBodyStyle.Wrapped WebMessageBodyStyle.WrappedReque ...

  2. javascript高级程序设计第四章 变量、作用域和内存问题

    变量包含两种,,基本类型和引用类型 基本类型是指一些简单的字段: 引用类型是☞由多个值构成的对象  引用类型的值是保存在内存中的对象,在javascript中是不允许直接访问内存中的位置; 函数的参数 ...

  3. sharepoint2013用户切换实现方式

    作为一个刚学sharepoint的新人,今天在账号的切换中烦躁无比,不知道有木有人和我一样,sharepoint2013没有了切换用户,真的很不方便,当然了,也不是没有办法加上去,经过本人一个下午的研 ...

  4. ASP.NET Core 在 Swagger UI 中显示自定义的 Header Token

    Swagger 是个好东西,对于前后端分离的网站来说,不仅是提高前后端开发人员沟通效率的利器,也大大方便了后端人员测试 API.有时候,API 中可能需要在 Header 中设置认证参数,比如 aut ...

  5. JAVA可阻塞队列-ArrayBlockingQueue

    在前面的的文章,写了一个带有缓冲区的队列,是用JAVA的Lock下的Condition实现的,但是JAVA类中提供了这项功能,就是ArrayBlockingQueue, ArrayBlockingQu ...

  6. python操作Excel--使用xlrd

    一.安装xlrd http://pypi.python.org/pypi/xlrd 二.使用介绍 1.导入模块 import xlrd 2.打开Excel文件读取数据 data = xlrd.open ...

  7. PS切图篇

    一.PS界面设置 1.新建(ctrl+n) 初始化尺寸参数 预设:自定 宽度:1920px 高度:自设(如:2000px) 分辨率:72像素/英寸 颜色:RGB/8位 背景内容:透明 存储为预设 2. ...

  8. 看守所、戒毒所3D指纹门禁系统解决方案

    为响应"科技强警"的战略方针,华本构建了一个完整的.集成的.可靠的.易操作的高安全性门禁系统,应用于看守所.戒毒所.公安局和部队等单位,使管理更现代化.规范化,有效地预防和制止越狱 ...

  9. 后台返回国标码,怎么转化为JSON

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; manager.responseSerializer = [AFHTTP ...

  10. Javascript中的链表

    function LinkedList() { // 辅助类,表示加入链表的每一项 var Node=function(element){ this.element=element; this.nex ...