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. 动态Pivot(1)

    原文 http://book.51cto.com/art/200710/58874.htm 7.7  动态Pivot 作为另外一个练习,假设你要编写一个存储过程,它生成动态Pivot查询.这个存储过程 ...

  2. Godiva_百度百科

    Godiva_百度百科 北京 三里屯 北京市朝阳区三里屯路19号院10号楼一层S10-13单元及二层S10-22单元 100027 北京朝阳大悦城北京市朝阳区朝阳北路101号朝阳大悦城1号商业楼1F- ...

  3. Primefaces的fileUpload组件使用

    最近在学习Primefaces(当然也是项目中需要用的).在使用其fileUpload遇到了不小的困难,现总结一下供大家及我自己今后参考使用. 1.首先是使用环境配置:正常的Primefaces开发环 ...

  4. 14.1.1 InnoDB as the Default MySQL Storage Engine

    14.1 Introduction to InnoDB 14.1.1 InnoDB as the Default MySQL Storage Engine 14.1.2 Checking InnoDB ...

  5. win 8.1 安装 SQL server 遇到的各种问题

    企业版 SQL Server ed2k://|file|cn_sql_server_2012_enterprise_edition_x86_x64_dvd_813295.iso|5054384128| ...

  6. 自定义NavgationBa返回按钮

    iOS  上UINavigationController视图压栈形式,可以在当前视图无限制push许多视图,然而一些会觉得自带的push按钮不够美观,而且当上的上一个页面title很长的时候,那个返回 ...

  7. ORACLE 安装Oracle12遇到的问题

    0.全然卸载Oracle10(Windows) 在Windows下多次安装Oracle会造成混乱.重装Oracle的话一定先要干净卸载曾经的Oracle. 一.有必要时先备份 二.卸载步骤 1.用DB ...

  8. CodeForces 21C Stripe 2 构造题

    题目链接: 题目链接:点击打开链接 #include <cstdio> #include <cstring> #include <algorithm> #inclu ...

  9. Java Design Demo -简单的队列-异步多任务队列(java android)

    简单的单线程队列 -- 工作的时候遇到劣质打印机.给打印机发消息,打印机就会打印,如果在打印机还在打印的时候,就 再发消息打印,就会出现消息丢失.所以需要给上一个任务一些处理的间隔时间. 单线程的消息 ...

  10. net析构函数对垃圾回收的影响

    net析构函数对垃圾回收的影响 之前忘了说了 代码都是在Release模式下运行的,现在补充上. 这里说析构函数,其实并不准确,应该叫Finalize函数,Finalize函数形式上和c++的析构函数 ...