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. 使用Java创建RESTful Web Service(转)

    REST是REpresentational State Transfer的缩写(一般中文翻译为表述性状态转移).2000年Roy Fielding博士在他的博士论文“Architectural Sty ...

  2. PHP $_SERVER

    $_SERVER 是一个包括了诸如头信息(header).路径(path).以及脚本位置(script locations)等等信息的数组.这个数组中的项目由 Web server创建.不能保证每一个 ...

  3. HDU 5046 Airport(DLX反复覆盖)

    HDU 5046 Airport 题目链接 题意:给定一些机场.要求选出K个机场,使得其它机场到其它机场的最大值最小 思路:二分+DLX反复覆盖去推断就可以 代码: #include <cstd ...

  4. Java内部类——局部内部类

    局部内部类,即放在“{块中}”,局部内部类,不能被成员修饰符static修饰,且内部类的成员也不能是static. 内部类中访问局部变量“{块中}”,需要将局部变量声明为final. 可以直接访问外部 ...

  5. Boost::asio io_service 实现分析

    io_service的作用 io_servie 实现了一个任务队列,这里的任务就是void(void)的函数.Io_servie最常用的两个接口是post和run,post向任务队列中投递任务,run ...

  6. HDU 3397 Sequence operation(线段树)

    HDU 3397 Sequence operation 题目链接 题意:给定一个01序列,有5种操作 0 a b [a.b]区间置为0 1 a b [a,b]区间置为1 2 a b [a,b]区间0变 ...

  7. hdu 4803 贪心/思维题

    http://acm.hdu.edu.cn/showproblem.php?pid=4803 话说C++还卡精度么?  G++  AC  C++ WA 我自己的贪心策略错了 -- 就是尽量下键,然后上 ...

  8. ASP.NET - 记录错误日志

    不需要像log4net/Nlog/Common Logging配置,简单好用. 不用增加声明logger对象,可记录当前执行状况. 可以定义 维护功能模板的开发人员,以便用功能模块对于开发人员. 出处 ...

  9. 利用ArrayList对Hashtable其进行排序

    前言: 最近在使用Hashtable的时候发现一个问题:就是当你对Hashtable进行遍历的时候整个输出结果是毫无顺序的, 上网查了一下说是Hashtable有自己内部的排序机制,如果要自定义排序的 ...

  10. 3篇OAuth的文章

    http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html http://blog.unvs.cn/archives/oauth-qq1.0-devel ...