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. Ubuntu 安装启动Tomcat

    首先下载ubuntu 的tar包 官网: http://tomcat.apache.org/download-80.cgi 安装启动 1 .下载对应的tar 2 .解压任意文件夹下,更改名字tomca ...

  2. transition与animation

    以前,一直都知道,transition是animation的一个简化版,甚至不算是动画,而是一种过渡. transition的用法 早两天用transition写了一个按钮滑动的效果,类似于IOS的设 ...

  3. WCF技术剖析之十:调用WCF服务的客户端应该如何进行异常处理

    原文:WCF技术剖析之十:调用WCF服务的客户端应该如何进行异常处理 在前面一片文章(服务代理不能得到及时关闭会有什么后果?)中,我们谈到及时关闭服务代理(Service Proxy)在一个高并发环境 ...

  4. 基于visual Studio2013解决面试题之1305字符串所有子集

     题目

  5. libevent简单介绍和使用

    <pre class="html" name="code">libevent接口的使用是简单easy的.关键还是一些其他技术须要深入了解.如epol ...

  6. Oracle的序列

    Oracle的序列 序列介绍 序列是Oracle提供的用于产生一系列唯一数字的数据库对象. 使用序列能够实现自己主动产生主键值.序列也能够在很多用户并发环境中使用.为所实用户生成不反复的顺序数字,并且 ...

  7. ASP.NET - 锚点跳转,用于回到顶部

    <a name ="top"></a> <a href ="#top">回到顶部</a> 第一行代码写在顶部,第 ...

  8. python for else

    http://blog.sina.com.cn/s/blog_5357c0af01013mwn.html raw_input() 与 input() __ Python http://www.cnbl ...

  9. cocos2d-x在win32和iOS、android下获取当前系统时间的方法

    最近在游戏里要显示当前系统时间的功能,网上一搜很多写着获取的方法,大都是如下 struct cc_timeval now; CCTime::gettimeofdayCocos2d(&now, ...

  10. Swift - 使用ALAssetsLibrary获取相簿里所有图片,视频(附样例)

    1,ALAssetsLibrary介绍 (1)通过创建ALAssetsLibrary的实例可以访问系统Photos里的图片与视频.这里图片不仅包括相机拍摄的照片,还包括从iTunes导入的和从其他设备 ...