最傻的方法:

    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. 用Paint Tool SAI绘制漫画

    漫画绘图软件 Paint Tool SAI是一个来自日本的小巧的漫画辅助绘图软件,只有11M大小. 这个没有任何现成的模板和组件,只能自己一笔一笔的话,画笔.图层等功能与Photoshop类似,但没有 ...

  2. 数据流 in redux

    之前用学过的react和react-router写了个单页面应用,但写完后总感觉还欠缺点什么,怎么说呢,就是感觉在数据流的控制上被框架约束的厉害,很不自由,比如兄弟组件间的通信就很不方便.然后才了解到 ...

  3. Spring学习笔记之二----基于XML的Spring AOP配置

    在Spring配置文件中,通常使用<aop:config>元素来设置AOP,其中应包括: <aop:aspect>指定aspect,aspect是一个POJO类,包含了很多的a ...

  4. java是值传递还是引用传递

    首先写一个简便的Employee,以便测试使用. class Employee { private String name; public Employee(String name) { this.n ...

  5. 《利用python进行数据分析》读书笔记--第七章 数据规整化:清理、转换、合并、重塑(三)

    http://www.cnblogs.com/batteryhp/p/5046433.html 5.示例:usda食品数据库 下面是一个具体的例子,书中最重要的就是例子. #-*- encoding: ...

  6. tensorflow安装日志(PIP)

    最近刚刚接触深度学习,安装一下tf 环境:华硕Z170主板.i7 6700k.GTX1070.Ubuntu16.04.Python2.7 在这之前先装好了cuda8.0.cudnn5.0.caffe整 ...

  7. electron打包发布

    1.全局安装electron npm install electron -g 在cmd 直接输入 electron 直接启electron 2.编写第一个Electron应用 在任何地方,建立一个ap ...

  8. angular的promise理解

    promise承诺是angular的一个重点部分,是一种异步处理值(或非值)的方法.一般程序是从上至下执行,遇到某个地方需要花点时间,就干等着把这个地方执行完了,才继续做下一个,看着有点笨蛋.而pro ...

  9. 【前端】从输入URL到页面加载完成的过程中都发生了什么事情

    把URL分割成几个部分:协议(http, https).网络地址(xxx.xxx.xxx.xxx).资源路径(/xxx/xxx.xx).端口号(默认80). 如果地址不是一个IP地址,通过DNS(域名 ...

  10. dev LayoutControl布局控件

    标题长短不一致,很难看 设置LayoutControl属性 效果: 修改字体: 控件字体: LayoutControl→Appearance→Control→Font→FontSize  (Contr ...