25. Reverse Nodes in k-Group
来源:互联网 发布:淘宝客推广开通条件 编辑:程序博客网 时间:2024/06/05 17:59
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; }}
0 0
- [LeetCode]25.Reverse Nodes in k-Group
- LeetCode --- 25. Reverse Nodes in k-Group
- LeetCode 25.Reverse Nodes in k-Group
- [Leetcode] 25. Reverse Nodes in k-Group
- 25. Reverse Nodes in k-Group
- 25. Reverse Nodes in k-Group
- 【LeetCode】25.Reverse Nodes in k-Group
- [leetcode] 25.Reverse Nodes in k-Group
- 25. Reverse Nodes in k-Group
- Leetcode-25.Reverse Nodes in k-Group
- 25. Reverse Nodes in k-Group
- 25. Reverse Nodes in k-Group
- 25. Reverse Nodes in k-Group
- 25. Reverse Nodes in k-Group
- 25. Reverse Nodes in k-Group
- leetcode 25. Reverse Nodes in k-Group
- leetcode 25. Reverse Nodes in k-Group
- 25. Reverse Nodes in k-Group
- Leetcode ☞ 8. String to Integer (atoi)
- iOS 性别封装
- HTML5特点
- cardboard SDK analysis
- IIC总线协议
- 25. Reverse Nodes in k-Group
- git入门
- ssh: connect to host github.com port 22: Connection refused
- Java 回顾笔记_对象初始化过程
- animation过渡效果
- Unknown host 'downloads.gradle.org'问题修改
- http请求
- Java回调机制解析
- 操作系统相关资料整理之一