Problem: 交换相邻的两个节点
 
 
  如上图所示,递归进行交换。从最尾端开始,当最尾端只有一个节点时,停止交换
  否则执行 swap(head.next) 
 
参考代码:
 
package leetcode_50;

/**
*
* @author pengfei_zheng
* 交换相邻节点
*/
public class Solution24 {
public class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
} public ListNode swapPairs(ListNode head) {
if ((head == null)||(head.next == null))
return head;
ListNode n = head.next;
head.next = swapPairs(head.next.next);
n.next = head;
return n;
}
}
 

LeetCode 24 Swap Nodes in Pairs (交换相邻节点)的更多相关文章

  1. [leetcode]24. Swap Nodes in Pairs交换链表的节点

    感觉这个题后台的运行程序有问题,一开始自己想的是反转链表那道题的方法,只是隔一个节点执行一次,但是没有通过,TLE了,但是很奇怪,并没有死循环,就是最后返回的时候超时. 最后的思路就是很简单的进行交换 ...

  2. [LeetCode] 24. Swap Nodes in Pairs ☆☆☆(链表,相邻两节点交换)

    Swap Nodes in Pairs 描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4 ...

  3. leetCode 24. Swap Nodes in Pairs (双数交换节点) 解题思路和方法

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

  4. LeetCode 24. Swap Nodes in Pairs(交换链表中每两个相邻节点)

    题意:交换链表中每两个相邻节点,不能修改节点的val值. 分析:递归.如果以第三个结点为头结点的链表已经两两交换完毕(这一步递归实现---swapPairs(head -> next -> ...

  5. Leetcode 24. Swap Nodes in Pairs(详细图解一看就会)

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

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

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

  7. LeetCode 024 Swap Nodes in Pairs 交换链表中相邻的两个节点

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

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

    题目标签:Linked List 题目给了我们一组 linked list,让我们把每对nodes 互换位置. 新键一个dummy node,然后遍历list,每次建立 s1 和 s2 记录两个点,然 ...

  9. [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 ...

随机推荐

  1. tp5数据输出

    法一:系统配置 'default_return_type'=>'json' 法二:输出设置 namespace app\index\controller; class Index { publi ...

  2. [NSURL URLWithString:] returns nil

    You need to escape the non-ASCII characters in your hardcoded URL as well: //localisationName is a a ...

  3. [译]Intel App Framework 3.0的变化

    App Framework 3.0 原文 IAN M. (Intel) 发布于 2015-02-11  05:24 我们高兴地宣布App Framework 的新版本3.0发布了.你可以获得最新的代码 ...

  4. 【Intel AF 2.1 学习笔记三】

    AF中自带一些Css矢量图标,使用时需要设置class,例如下面代码显示一个左箭头图标: <a class="icon left"></a> AF中可用的图 ...

  5. jsTree 插件Ajax数据

    完整代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta ...

  6. virtualbox谨记:续....

    接“virtualbox谨记:win7上只有4.3.x的版本支持ubuntu14.04.3虚拟机安装Oracle Rac,其他的版本3.x和5.0.2(至2015-08-30)均不可以”, 续 me自 ...

  7. MyEclipse使用笔记

    简单记录下个人常用的一些MyEclipse设置 VS颜色方案 Window-->Preference-->Java->Editor-->Syntax Coloring Clas ...

  8. jenkins 升级jdk到1.8.0 报java.io.IOException:Unable to read /var/lib/jenkins/config.xml

    今天手动下载安装了jdk1.8.0, 并修改了配置文件,当前默认使用该版本的jdk.但是报出一下错误: 问题查到: https://issues.jenkins-ci.org/browse/JENKI ...

  9. 转:Hibernate query.list()之卡住问题

    某个函数里面有调用Query的list()方法,然后它有时会出现这种症状: 忽然停住不动,但是也没报异常,就是界面死了. 我的查询差不多是这样: Query q=sessionFactory.open ...

  10. css段落首字母下沉

    摘要: 段落首字母放大是指放大段落开头的字母或者汉字,主要使用了css的first-letter伪类选择器. 单行放大: 在第一行内放大,效果如下: <!DOCTYPE html> < ...