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. 【Php】数组遍历,foreach, each, trim()

    <?php $iplist = "122.224.251.154|192.168.2.138|192.168.2.12"; echo $_SERVER['REMOTE_ADD ...

  2. koa2使用阿里云oss的nodejs sdk实现上传图片

    nodejs实现上传图片到阿里云,自然是写成接口形式比较方便,前端监听input file的改变,把file对象传入到formData中传入后端,不能直接传入file对象,后端需要接受formData ...

  3. Android:AS与Unity3D之间打包的各种坑及解决方案

    作者:DrkCore (http://blog.csdn.net/DrkCore) 原文链接:(http://blog.csdn.net/drkcore/article/details/5207937 ...

  4. Linux中什么是块设备 及 lsblk命令的使用

    Linux中I/O设备分为两类:字符设备和块设备.两种设备本身没有严格限制,但是,基于不同的功能进行了分类.(1)字符设备:提供连续的数据流,应用程序可以顺序读取,通常不支持随机存取.相反,此类设备支 ...

  5. WCF+Nhibernate循环引用导致序列化的问题

    WCF+Nhibernate 在查询中只要涉及到表关联,都会存在一个循环引用而导致客户端不能反序列化的问题. 解决的办法: 1.多对一关联中,设置lazy="false". 2.一 ...

  6. SpringBoot------热部署(Springloaded)

    为啥要热部署: 在修改代码的时候,不需要重新启动程序,程序会自动进行编译 注意: 控制器中新增加的方法是不能进行热部署的 方法: 1.在pom.xml文件里面添加下面代码 <project> ...

  7. Go之获取Windows下文件是否隐藏

    起初,做了个小程序,用来检测磁盘中所有的文件 package main import( "fmt" "io/ioutil" "os" ) v ...

  8. zabbix中Templates的jmx相关key调试方法

    1.下载 cmdline jmxclient 如果你有一个完美的模版,你可能可以忽略此步.但是大多数情况下你没有.况且 zabbix 默认的 tomcat 模版也不能很好的工作.这时候有一个工具来调试 ...

  9. 【数据分析】Superset 之四 直接安装

    apt install python-pip pip install --upgrade pip apt-get install build-essential libssl-dev libffi-d ...

  10. Data Guard 主备库角色转换

    1. switchover操作 1.1 备库先关闭实时日志应用 standby>alter database recover managed standby database cancel; 1 ...