24. Swap Nodes in Pairs | Java最短代码实现
2016-04-09 09:48
609 查看
原题链接:24. Swap Nodes in Pairs
【思路】
本题考查节点的基本操作。每轮循环,如果存在2个节点,则交换前后位置,用p、q两个指针来完成;如果只存在1个节点,则不交换前后位置:
cases passed. Runtime: 0 ms Your runtime beats 13.35% of javasubmissions.
【思路】
本题考查节点的基本操作。每轮循环,如果存在2个节点,则交换前后位置,用p、q两个指针来完成;如果只存在1个节点,则不交换前后位置:
public ListNode swapPairs(ListNode head) { if (head == null || head.next == null) return head; ListNode p = new ListNode(0), q = p; p.next = head; head = head.next; while ((q = q.next) != null && q.next != null) { //说明至少存在2个节点待交换,则进行节点交换 p.next = q.next; q.next = p.next.next; p.next.next = q; p = q; } return head; }55 / 55 test
cases passed. Runtime: 0 ms Your runtime beats 13.35% of javasubmissions.
相关文章推荐
- nodejs+Ajax访问路由,返回json数据
- mongoose的在node中的使用。入门
- Node.js介绍
- Node.js-2.模块
- leetcode: 237. Delete Node in a Linked List
- FILE、inode、fork和vfork
- Node.js中,获取req请求的原始IP
- 浅谈inode
- 一个Java码农的Node之旅
- SpriteKit-(SKNode)
- nodemailer实现node发送邮件
- inode简单理解
- node.js安装方法总结
- Reverse Nodes in k-Group(与leetcode不同)
- lightoj 1257 - Farthest Nodes in a Tree (II) 树直径
- nodejs错误 : request entity too large
- [置顶] node-webkit支持sqlite
- 开源虚拟化解决方案ovirt实验(2.ovirt-node安装)
- kafka创建topic报错:KeeperException$NoNodeException: KeeperErrorCode = NoNode for /brokers/ids
- 几行代码搞定nodejs中的回调坑