24. Swap Nodes in Pairs
2016-04-17 10:56
375 查看
Given a linked list, swap every two adjacent nodes and return its head.
For example,
Given
Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
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.
public class Solution{ public ListNode swapPairs(ListNode head) { if (head == null || head.next == null) return head; ListNode second = head.next; ListNode third = second.next; second.next = head; head.next = swapPairs(third); return second; } } //从题目的意思上看,就知道这题目有很多重复性的动作,可以通过递归来解决,不难~
相关文章推荐
- NodeJs的express模块4.X
- NodeJS + Socket.IO 最终版
- EasyUI Tree 中的Node state属性
- [Lintcode]Swap Nodes in Pairs
- 111.Populating Next Right Pointers in Each Node
- 【Node JS环境搭建及sublime Text 3配置Node Js环境】
- Nodejs Stream 数据流使用手册
- 用nodejs模拟请求发微博
- 【一天一道LeetCode】#19. Remove Nth Node From End of List
- 【一天一道LeetCode】#19. Remove Nth Node From End of List
- nodejs cms开源框架
- Node.js 文件模块(fs)模块介绍
- 配置nodejs环境
- 详细分析Node.js中的UDP模块
- node初识
- windows环境安装nodejs
- node.js 学习笔记003 :使用superagent和cheerio实现简单网页爬虫
- ubuntu 源码安装nodejs
- 安装Node.js和Electron
- nodejs爬虫编码问题