LeetCode -- Reverse Nodes in k-Group
2015-10-30 14:40
573 查看
题目描述:
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; } } }
相关文章推荐
- nodeJS第二章
- Node.js使用模块简单例子
- 用requirejs、socket.io、nodejs来传送页面所需的js、css
- ajax与nodejs实现小交互(html,txt)
- node的事件模块应用(译)
- NodeJs第一章
- hadoop 根据SecondaryNameNode恢复Namenode
- nodejs之gulp自动化构建工具
- NodeJS的异步编程风格
- node.js的作用、回调、同步异步代码、事件循环
- 在jQuery与Node之间创建跨域请求(Cross-Origin Resource Sharing (CORS) )
- Populating Next Right Pointers in Each Node II
- (java)leetcode Delete Node in a Linked List
- Nodejs
- NodeJS错误-throw er; // Unhandled 'error' event
- Populating Next Right Pointers in Each Node
- 命令passwd报错因inode节点处理记录
- node 使用get和post向后台请求数据的使用方式对比
- Node.js 安装与配置
- nodejs 中引用css和js的路径问题