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. HTML5重力感应小球冲撞动画实现教程

    今天我们来分享一款很酷的HTML5重力感应动画教程,这款动画可以让你甩动页面中的小球,小球的大小都不同,并且鼠标点击空白区域时又可以生成一定数量的小球.当我们甩动小球时,各个小球之间就会发生互相碰撞的 ...

  2. php中对象(object)与数组(array)之间的相互转换

    /** * 数组 转 对象 * * @param array $arr 数组 * @return object */ function array_to_object($arr) { if (gett ...

  3. 最新Java面试题及答案整理

    基础篇 一.基本功 面向对象特征 封装,继承,多态和抽象 1. 封装 封装给对象提供了隐藏内部特性和行为的能力.对象提供一些能被其他对象访问的方法来改变它内部的数据.在 Java 当中,有 3 种修饰 ...

  4. lua:值得看的博客资源 ...

    凯奥斯 :https://blog.csdn.net/ecidevilin/article/category/6454847 https://blog.csdn.net/qinyuanpei/arti ...

  5. JS_SINA股票接口

    深成指: <script type="text/javascript" src="http://hq.sinajs.cn/list=sz399001" c ...

  6. php解析出带层级关系的mpp文件

    本来要使用DHX gantt插件自带的API做导入,可是做完后,又发现不稳定,不能访问了 可能是屏蔽掉了 所以又想起可以使用javaBridge,借用java的MPXJ php解析mpp的 上一篇介绍 ...

  7. BarTender中每个标签提示手动输入的设置方法

    我们知道手动输入数据进行标签打印,可以利用BarTender中的数据输入表单来实现.表单的创建有利于提示程序员,输入要打印标签的的数据.如果手动输入数据的标签较多,可以设置表单提示为每个标签提示,减去 ...

  8. QT编译错误:undefined reference to `__imp_gl*'等等

    学习QT OpenGL绘制图形,程序中使用了OpenGL的API函数(gl开头),但是编译出现了错误:截图如下 有过编程经验的人可知,是链接的时候出错,找不到函数的实现! 解决方法:在工程*.pro文 ...

  9. ubuntu下安装程序的五种方法

    在ubuntu当中,安装应用程序我所知道的有三种方法,分别是apt-get,dpkg安装deb和make install安装源码包三种.下面针对每一种方法各举例来说明. 一.apt-get方法 使用a ...

  10. html 内联函数宽度设置

    width and/or height in tables are not standard anymore; as Ianzz says, they are depreciated. Instead ...