最傻的方法:

    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. Swift_UI_UIButton

    class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // 1. 自定义 ...

  2. js+jquery的等价用法

    js: 获取属性的值: document.getElementById("id").value; 设置属性的样式: document.getElementById("id ...

  3. Live2d-cocos2dx教程(一)例子搭建及运行

    前言 这篇文章不讲代码,介绍live2d-cocos2dx-sdk 的下载.配置运行官网例子以及遇到的问题解决方案.第一次接触这个,有错的地方,希望大神指正.目前cocos2dx-live2d资料很少 ...

  4. 初学者-微信小程序 问题解决办法记录

    1.tabBar不显示的问题 1),检查大小写 2),pagePath路径书写,和pages路径一样,不能多或者少一个"/"或者"?" 2.tabBar和nav ...

  5. Topcoder SRM 683 Div2 B

    贪心的题,从左向右推过去即可 #include <vector> #include <list> #include <map> #include <set&g ...

  6. Candies-POJ3159差分约束

    Time Limit: 1500MS Memory Limit: 131072K Description During the kindergarten days, flymouse was the ...

  7. ipad pro 文章

    这篇文章是通过iPad Pro发送的.体验一下键盘输入,以及safari下的输入.这个键盘的输入手感好一般,按键行程较短.

  8. AndroidManifest.xml详解(上)

    本文编辑整理自:http://blog.163.com/hero_213/blog/static/39891214201242835410742/ 一.关于AndroidManifest.xml    ...

  9. Socket之TCP连接_TcpNoDelay

    摘自: http://jerrypeng.me/2013/08/mythical-40ms-delay-and-tcp-nodelay/

  10. 简介python2.x的编码

    python2.x的中文编码真是令人头痛,简单写下自己的一点python编码转换的体会. windows平台用的默认编码格式为gbk >>> s = raw_input() #在wi ...