25. Reverse Nodes in k-Group
2016-02-29 14:59
603 查看
Reverse Nodes in k-Group
For example:Given this linked list: 1->2->3->4->5
For k = 2, you should return: 2->1->4->3->5
For k = 3, you should return: 3->2->1->4->5
代码:
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution { public ListNode reverseKGroup(ListNode head, int k) { ListNode shao = head; int count = 0; while(shao != null && count != k){ //寻找k+1个节点 shao = shao.next; count++; } if(count == k){ shao = reverseKGroup(shao,k); //使k+1的节点作为shao的头节点 while(count-- > 0){ //需翻转的次数 ListNode tmp = head.next; head.next = shao; shao = head; head = tmp; } head = shao; } return head; } }
相关文章推荐
- Node.js 手册查询-1-核心模块方法
- Node.js 手册查询-3-Mongoose 方法
- Node.js 手册查询-5-Ejs 方法
- Node.js 手册查询-4-Express 方法
- Node.js ejs中文手册
- <node> tag
- 阿里云ECS安装最新版本Node.js
- nw.js node-webkit系列(10)Native UI API Clipboard的使用
- Vertex与Node
- Pitfall in node crypto and base64 encoding
- 19. Remove Nth Node From End of List
- Node.js Web 模块-创建web服务器
- Node.js GET/POST请求
- Node.js 文件系统
- Node.js 函数
- Node.js模块系统
- Node.js Stream(流)
- Node.js Buffer(缓冲区)
- Node.js EventEmitter
- Node.js的基础内容