25. Reverse Nodes in k-Group 还未解决!!!
来源:互联网 发布:文明5 贸易网络 编辑:程序博客网 时间:2024/06/08 03:52
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple ofk then left-out nodes in the end should remain as it is.
You may not alter the values in the nodes, only nodes itself may be changed.
Only constant memory is allowed.
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
public ListNode reverseKGroup(ListNode head, int k) { ListNode curr = head; int count = 0; while (curr != null && count != k) { // find the k+1 node curr = curr.next; count++; } if (count == k) { // if k+1 node is found curr = reverseKGroup(curr, k); // reverse list with k+1 node as head // head - head-pointer to direct part, // curr - head-pointer to reversed part; while (count-- > 0) { // reverse current k-group: ListNode tmp = head.next; // tmp - next head in direct part head.next = curr; // preappending "direct" head to the reversed list curr = head; // move head of reversed part to a new node head = tmp; // move "direct" head to the next node in direct part } head = curr; } return head;}
阅读全文
0 0
- 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
- [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
- 魔法城市路径问题(腾讯2017校招笔试题)
- 将原始图片转换成TFRecord文件
- LintCode解题记录-Catalan Number
- 平滑滤波器的演化
- 兼容多种浏览器的,多行文本多余显示省略号(···)的方法
- 25. Reverse Nodes in k-Group 还未解决!!!
- 组件间通信 angualr4中间人模式
- 动画速度的控制
- poj3259 luogu2850 虫洞问题
- java深入学习六之正则表达式
- Python 中的random函数
- Apache Beam的分窗与触发器
- Windows系统中搭建Spark开发环境
- Qt实现一个简单的打字游戏