Leetcode_reverse-nodes-in-k-group
2014-03-23 20:49
369 查看
地址:http://oj.leetcode.com/problems/reverse-nodes-in-k-group/
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:
For k = 2, you should return:
For k = 3, you should return:
思路:题目里说了只能用常量的额外空间。所以就用一个vector,容量为K,满了K个就反序输出,否则顺序输出。另外注意边界条件,比如k=1或者链表长度小于K的话可以直接输出原链表。
参考代码:
//SECOND TRIAL, without extra spaceclass Solution {public: ListNode *reverseKGroup(ListNode *head, int k) { int len = 0; ListNode* p = head, *pre = NULL, *h = NULL, *nxt = NULL, *lastp = NULL, *curp = NULL; while(p) { ++len; p = p->next; } if(len < k || k<=1) return head; p = head; while(len >= k) { len -= k; curp = p; for(int i = 0; i<k; ++i) { nxt = p->next; p->next = pre; pre = p; p = nxt; } if (!h) h = pre; else lastp->next = pre; lastp = curp; pre = NULL; } if(p) lastp->next = p; return h; }};
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
思路:题目里说了只能用常量的额外空间。所以就用一个vector,容量为K,满了K个就反序输出,否则顺序输出。另外注意边界条件,比如k=1或者链表长度小于K的话可以直接输出原链表。
参考代码:
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode *reverseKGroup(ListNode *head, int k) { if(!head) return NULL; if(k<=1) return head; vector<ListNode*> vec; ListNode* p = head, *newHead=NULL, *q=NULL; while(p) { while(p && vec.size()<k) { vec.push_back(p); p = p->next; } if(vec.size()==k) { if(!newHead) { q = newHead = vec.back(); vec.pop_back(); } while(!vec.empty()) { q->next = vec.back(); vec.pop_back(); q = q->next; } } else { if(!newHead) { return head; } else { for(int i = 0; i < vec.size(); ++i) { q->next = vec[i]; q = q->next; } } } } q->next = NULL; return newHead; } };
//SECOND TRIAL, without extra spaceclass Solution {public: ListNode *reverseKGroup(ListNode *head, int k) { int len = 0; ListNode* p = head, *pre = NULL, *h = NULL, *nxt = NULL, *lastp = NULL, *curp = NULL; while(p) { ++len; p = p->next; } if(len < k || k<=1) return head; p = head; while(len >= k) { len -= k; curp = p; for(int i = 0; i<k; ++i) { nxt = p->next; p->next = pre; pre = p; p = nxt; } if (!h) h = pre; else lastp->next = pre; lastp = curp; pre = NULL; } if(p) lastp->next = p; return h; }};
相关文章推荐
- 用package.json配置NodeJS项目的模块声明
- Hadoop源码分析之读文件时NameNode和DataNode的处理过程
- LeetCode Populating Next Right Pointers in Each Node
- hadoop的datanode启动不起来
- LeetCode之Remove Nth Node From End of List
- 08、开源游戏-“胡子”开始游戏前02
- 从HTML中调用Node.js
- LeetCode之Reverse Nodes in k-Group
- NodeJS学习--部署环境
- ToS Violation - Defaced Site 严格的linode
- JS学习之Node类型
- Node.js中的exports与module.exports的区分
- LeetCode Remove Nth Node From End of List
- nodejs jsonp跨域使用 服务器端
- LeetCode Swap Nodes in Pairs
- 读《了不起的NodeJS》笔记 - nodejs 包管理(NPM)
- LeetCode之Swap Nodes in Pairs
- Ahjesus Nodejs02 使用集成开发环境
- 在Windows平台上安装Node.js及NPM模块管理
- NodeJS学习笔记(一)