LeetCode -- Reverse Nodes in k-Group
来源:互联网 发布:拍电影知乎 编辑:程序博客网 时间:2024/06/06 05:12
题目描述:
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
If the number of nodes is not a multiple of k 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
就是在遍历链表的过程中,如果从当前节点开始算,节点数大于k个,反转这k个节点;然后从当前位置向后移动k个位置,继续反转的过程,直到从当前节点到最后的节点数小于K个停止反转。
思路:
1. 创建反转函数,反转从当前开始的k个节点。 这个函数空间复杂度O(n)
2. 先得到链表总长度,如果小于k直接返回,否则每次旋转k个节点并指向下一次旋转的位置。
实现代码:
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
If the number of nodes is not a multiple of k 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
就是在遍历链表的过程中,如果从当前节点开始算,节点数大于k个,反转这k个节点;然后从当前位置向后移动k个位置,继续反转的过程,直到从当前节点到最后的节点数小于K个停止反转。
思路:
1. 创建反转函数,反转从当前开始的k个节点。 这个函数空间复杂度O(n)
2. 先得到链表总长度,如果小于k直接返回,否则每次旋转k个节点并指向下一次旋转的位置。
实现代码:
/** * Definition for singly-linked list. * public class ListNode { * public int val; * public ListNode next; * public ListNode(int x) { val = x; } * } */public class Solution { public ListNode ReverseKGroup(ListNode head, int k) { if(head == null || head.next == null || k == 1){ return head; } var len = 0; var h = head; while(head != null){ head = head.next; len ++; } if(k > len){ return h; } ListNode pre = null; ListNode newHead = null; while(len >= k) { ReverseKNodes(ref h, k, ref pre); if(pre == null){ newHead = h; pre = h; for(var i =0 ;i < k-1; i++){ pre = pre.next; } }else{ for(var i = 0;i < k;i++){ pre = pre.next; } } for(var i = 0;i < k;i++){ h = h.next; } len -= k; } return newHead; }private void ReverseKNodes(ref ListNode n, int k, ref ListNode preNode){var stack = new Stack<int>();for(var i = 0;i < k; i++){stack.Push(n.val);n = n.next;}ListNode end = n;ListNode tmp = new ListNode(stack.Pop());var tmpHead = tmp;while(stack.Count > 0){var n1 = new ListNode(stack.Pop());tmp.next = n1;tmp = tmp.next;}tmp.next = end;if(preNode != null){n = tmpHead;preNode.next = tmpHead;}else{n = tmpHead;}}}
1 0
- LeetCode:Reverse Nodes in k-Group
- LeetCode: Reverse Nodes in k-Group
- LeetCode : Reverse Nodes in k-Group
- leetcode 42: Reverse Nodes in k-Group
- [Leetcode] reverse nodes in K-Group
- [LeetCode]Reverse Nodes in k-Group
- LeetCode-Reverse Nodes in k-Group
- LeetCode - Reverse Nodes in k-Group
- LeetCode | Reverse Nodes in k-Group
- 【leetcode】Reverse Nodes in k-Group
- Leetcode: Reverse Nodes in k-Group
- leetcode Reverse Nodes in k-Group
- [LeetCode] Reverse Nodes in k-Group
- LeetCode题解: Reverse Nodes in k-Group
- 【Leetcode】Reverse Nodes in k-Group
- LeetCode Reverse Nodes in k-Group
- [Leetcode] Reverse Nodes in k-Group (Java)
- LeetCode(25)Reverse Nodes in K-Group
- 分享使用Unity 3D免费版本编写游戏的教程
- 友盟多渠道获取渠道名 附赠多渠道打包文件
- 让生成的程序默认有管理员权限
- Android多个依赖项目Ant编译命令
- XMLStarlet 命令行操作xml利器
- LeetCode -- Reverse Nodes in k-Group
- Android网络通信的六种方式
- Git学习教程(分支与合并)
- ios俩个APP之间跳转、传值
- linux解决文件名乱码
- mysql模糊查询
- Android MediaPlayer各种状态切换
- 虚拟机ubuntu系统出现的循环登录界面解决方法
- 使用jQuery Validate相关问题