最傻的方法:

    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. Java 邮件发送

    <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId&g ...

  2. DragRow-GYF

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DragRowDemo.as ...

  3. eclipse构建maven+scala+spark工程 转载

    转载地址:http://jingpin.jikexueyuan.com/article/47043.html 本文先叙述如何配置eclipse中maven+scala的开发环境,之后,叙述如何实现sp ...

  4. IOS 修改UIAlertController的按钮标题的字体颜色,字号,内容

    IOS 修改UIAlertController的按钮标题的字体颜色,字号,内容 UIAlertController *alertVC = [UIAlertController alertControl ...

  5. 设置button键隐藏文字text

     新手的第一天,从最简单的view开始做起.首先先来做个最简单的button携带不可见文本,想要的时候,get它就行了. 编程的都知道,get .set方法,就不多介绍了. 创建一个类,MyButto ...

  6. sqlite嵌入式数据库C语言基本操作(1)

    sqlite嵌入式数据库C语言基本操作(1) :first-child{margin-top:0!important}img.plugin{box-shadow:0 1px 3px rgba(0,0, ...

  7. 【Web】URI和URL,及URL的编码

    URI和URL是什么,以及他们的区别 URL,Uniform Resource Locator,统一资源定位符.用于表示网络上服务器的资源所在位置,比如我们输入浏览器的地址. URI,Uniform ...

  8. 27-React Lists and Keys

    Lists and Keys React支持以数组的形式来渲染多个组件,它会将你数组中的每个组件以列表的形式渲染开来. 当你使用数组的方式来渲染你的组件时,你需要给每个组件一个Key值,否则会出现一个 ...

  9. servlet(二)

    一.ServletConfig讲解 1.1.配置Servlet初始化参数 在Servlet的配置文件web.xml中,可以使用一个或多个<init-param>标签为servlet配置一些 ...

  10. PL/SQL通过免安装客户端连接远端ORACLE数据库

    参考百度经验:http://jingyan.baidu.com/article/375c8e19b4094d25f2a2291a.html