最傻的方法:

  1. ListNode *swapPairs(ListNode *head) {
  2. if (head == NULL)
  3. return NULL;
  4. ListNode *temp = );
  5. ListNode *head_2 = temp;
  6. while (head != NULL && head->next != NULL) {
  7. temp = temp->next = new ListNode(head->next->val);
  8. temp = temp->next = new ListNode(head->val);
  9. head = head->next->next;
  10. }
  11. if (head != NULL)
  12. temp->next = head;
  13. return head_2->next;
  14. }

好一点的方法

  1. ListNode* swapPairs(ListNode* head) {
  2. if(!head || !head->next)
  3. return head;
  4. ListNode * temp = head->next;
  5. head->next = swapPairs(temp->next);
  6. temp->next = head;
  7. return temp;
  8. }

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. iOS 解决图片上传到服务器旋转90度的问题(图片倒置)

    //使用swift的朋友们可以,把这个所在的类的.h,在-Header-Swift.h中一用一下. - (UIImage *)fixOrientation:(UIImage *)aImage { if ...

  2. WebLogic集群体系架构

    WebLogic Server集群概述  WebLogic Server 群集由多个 WebLogic Server 服务器实例组成,这些服务器实例同时运行并一起工作以提高可缩放性和可靠性.对于客户端 ...

  3. 005_重写 Standard Delete Button

    以后会用JS直接删除,但是在加载.js时候出现问题,会在以后进一步追踪完善: <apex:page standardController="Opportunity" > ...

  4. Codefroces 750C:New Year and Rating(思维)

    http://codeforces.com/contest/750/problem/C 题意:有n场比赛,每场比赛有一个c,代表比赛结束后分数的增长情况,有一个d,代表这场比赛在div1或者div2打 ...

  5. Android4.0 添加一个新的Android 键值

    这里添加新的键值,不是毫无凭据凭空创造的一个键值,而是根据kernel中检测到的按键值,然后转化为Android所需要的数值: 以添加一个Linux键值为217,把它映射为android的键值Brow ...

  6. javascript数据属性和访问器属性

    var book={ _year:2004, edition:1};Object.defineProperty(book,"year",{ get:function(){ retu ...

  7. js 两个滚动事件相互影响

    document.addEventListener('scroll', function(event) { if (event.target.id === 't_r_content') { // or ...

  8. js 、jsdoc生成33

    ============== js 点击事件后没方法名,调用有方法名 document.getElementById('lind').onclick=abc;//传统的id选择器 中没有# 哦 fun ...

  9. 年终汇报、总结、述职:教你做一场B格满满的技术大会演讲

    什么样的演讲和呈现最受听众欢迎,内容干货?逻辑清晰?长相帅气? 偶尔被邀作为speaker参加一些圈内的技术大会进行演讲.这里我分享下自己的经验,如何做一场B格满满的技术大会演讲,希望给做汇报.总结. ...

  10. Hibernate 继承映射

    @Entity@Inheritance(strategy=InheritanceType.SINGLE_TABLE)@DiscriminatorColumn()public class Animal ...