Given a linked list, swap every two adjacent nodes and return its head.

For example,

Given 1->2->3->4, you should return the list as 2->1->4->3.

Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.

思路:肯定是递归方便啦,我们做好了base case,一直调用自己就能够了。 要是想不出来base case是什么,多理解理解就熟悉了

[java] view
plain
copy

  1. public ListNode swapPairs(ListNode head) {
  2. if(head==null) return null;
  3. if(head.next==null) return head;
  4. ListNode temp=head.next;
  5. ListNode forward=head.next.next;
  6. temp.next=head;
  7. head.next=swapPairs(forward);
  8. return temp;
  9. }

[LeetCode]Swap Nodes in Pairs 成对交换的更多相关文章

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

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

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

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

  3. [LeetCode]Swap Nodes in Pairs题解

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

  4. [LintCode] Swap Nodes in Pairs 成对交换节点

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

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

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

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

    Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...

  7. LeetCode 24. Swap Nodes in Pairs 成对交换节点 C++/Java

    Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...

  8. [LeetCode]24. Swap Nodes in Pairs两两交换链表中的节点

    Given a linked list, swap every two adjacent nodes and return its head. Example: Given 1->2->3 ...

  9. 【LeetCode】Swap Nodes in Pairs(两两交换链表中的节点)

    这是LeetCode里的第24题. 题目要求: 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定1->2->3->4, 你应该返回2->1->4- ...

随机推荐

  1. SQL模板和模板实例化

    需求:需要得出一个数据源DataTable,我已知SQL和HttpRequest如何,通过SQL模板的方式去实例化匹配HttpRequest中的参数实例化为查询SQL,最后返回DataTable 1. ...

  2. 基于visual Studio2013解决C语言竞赛题之1028平均值

          题目 解决代码及点评 /* 已知有9个数,请求出这些数中的最大值.最小值及平均值,以及有多少个数等于平均值? */ #include<stdio.h> ...

  3. One simple health check for oracle with sql

    There are some sqls which is used for check the oracle database's health condition. ------numbers of ...

  4. 使用最新的log4cplus(1.1.1)隔离不同的 log 文件输出

    部分参考了博客. http://www.cppblog.com/tx7do/articles/11719.html 基于脚本配置来过滤log信息 除了通过程序实现对log环境的配置之外,log4cpl ...

  5. nest expression &amp; Pyparsing

    http://pyparsing.wikispaces.com/ http://bbs.csdn.net/topics/330052586 C++ boost "<([^<> ...

  6. sqlserver 存储过程实例

    ALTER PROC [dbo].[SP_mm_NS] (        @ID        NVARCHAR(60),        @ReturnCode    NVARCHAR(30) OUT ...

  7. scrum经验

    Scrum是基于过程控制理论的经验方法,倡导自组织团队:其运行框架核心是迭代增量型并行开发,也是“适应性”的软件开发方法.Scrum提供了高度可视化的用于管理软件开发复杂性管理的敏捷项目管理的实践框架 ...

  8. Entity - 使用EF框架进行增删改查 - 数据库先行

    数据库先行:先创建数据库,然后进行增删查该操作. 要操作的表结构(表名:Tb_Category): 创建一个控制台程序: 添加一个ADO.NET实体数据模型: 1.对控制台程序右键 2.选择ADO.N ...

  9. 一个简单的webdynpro的ALV示例

    开发alv的时候需要1.在web dynpro组件下面 的已使用的组件中添加ALV组件 SALV_WD_TABLE 2.在组件控制器的属性下面创建ALV组件SALV_WD_TABLE 3.在视图界面的 ...

  10. Ubuntu_文件夹名字转化成英文

    打开终端命令行输入: export LANG=en_US xdg-user-dirs-gtk-update 之后重启,就看到中文的文件夹变成英文的了 想要换回中文的输入: export LANG=zh ...