LeetCode 25:Reverse Nodes in k-Group
2015-11-28 00:48
816 查看
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:
Subscribe to see which companies asked this question
方法一:利用栈结构
完整测试代码:
方法二:不利用栈结构,直接在原链表中调整
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
Subscribe to see which companies asked this question
方法一:利用栈结构
//利用一个栈来实现每k个结点的逆序, //每组逆序结束后,将每组最后一个结点与下一组第一个结点重新链接 //例如:对单链表1->2->3->4->5->6->7->8->9进行每4个结点一组进行逆序 class Solution{ public: ListNode* reverseKGroup(ListNode* head, int k) { if (head == NULL || head->next == NULL || k < 2) return head; stack<ListNode*> stack1; ListNode *newHead = head; ListNode *cur = head; ListNode *pre = NULL; ListNode *next = NULL; while (cur != NULL) { next = cur->next; stack1.push(cur); //将结点依次入栈 if (stack1.size() == k) //当入栈结点达到k个时,这k个结点开始逆序 { pre = resign1(stack1, pre, next); //pre=cur1 第一组结点逆序后,pre指向结点1 while (!stack1.empty()) //每组逆序后,将stack清空 { stack1.pop(); } newHead = newHead == head ? cur : newHead; //记录新的头结点,第一组全部入栈后,cur指向结点4, //第一组结点逆序后,新头结点变为4 } cur = next; //第一组逆序结束后,cur重新指向下一组的第一个结点5,开始遍历下一组 } return newHead; } //将k个结点按照出栈顺序依次重新链接(比如:原单链表1->2->3->4,出栈后变成4->3->2->1) ListNode * resign1(stack<ListNode*>stack2, ListNode* left, ListNode* right){ ListNode *cur1 = stack2.top(); if (left != NULL) { left->next = cur1; } ListNode *next = NULL; while (!stack2.empty()) { next = stack2.top( ); cur1->next = next; //依次重新链接 cur1 = next; stack2.pop(); } cur1->next = right; //1->2->3->4,出栈后变成4->3->2->1后,cur1指向结点1,重新链接上结点5 //1->2->3->4->5->6->7->8->9变为4->3->2->1->5->6->7->8->9 return cur1; } };
完整测试代码:
#include <iostream> #include<stack> #include<list> using namespace std; struct ListNode{ int data; ListNode *next; ListNode(int x) :data(x), next(NULL){} }; //利用一个栈来实现每k个结点的逆序, //每组逆序结束后,将每组最后一个结点与下一组第一个结点重新链接 class Solution{ public: ListNode* reverseKGroup(ListNode* head, int k) { if (head == NULL || head->next == NULL || k < 2) return head; stack<ListNode*> stack1; ListNode *newHead = head; ListNode *cur = head; ListNode *pre = NULL; ListNode *next = NULL; while (cur != NULL) { next = cur->next; stack1.push(cur); //将结点依次入栈 if (stack1.size() == k) //当入栈结点达到k个时,这k个结点开始逆序 { pre = resign1(stack1, pre, next); //pre=cur1 第一组结点逆序后,pre指向结点1 while (!stack1.empty()) //每组逆序后,将stack清空 { stack1.pop(); } newHead = newHead == head ? cur : newHead; //记录新的头结点,第一组全部入栈后,cur指向结点4, //第一组结点逆序后,新头结点变为4 } cur = next; //第一组逆序结束后,cur重新指向下一组的第一个结点5,开始遍历下一组 } return newHead; } //将k个结点按照出栈顺序依次重新链接(比如:原单链表1->2->3->4,出栈后变成4->3->2->1) ListNode * resign1(stack<ListNode*>stack2, ListNode* left, ListNode* right){ ListNode *cur1 = stack2.top(); if (left != NULL) { left->next = cur1; } ListNode *next = NULL; while (!stack2.empty()) { next = stack2.top( ); cur1->next = next; //依次重新链接 cur1 = next; stack2.pop(); } cur1->next = right; //1->2->3->4,出栈后变成4->3->2->1后,cur1指向结点1,重新链接上结点5 //1->2->3->4->5->6->7->8->9变为4->3->2->1->5->6->7->8->9 return cur1; } }; //创建只有空链表,返回一个dummy节点 ListNode * createList() { ListNode *dummy = new ListNode(-1); return dummy; } ListNode * initList(ListNode *dummy) { //用一个数组初始化链表 int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; ListNode *tmp, *head; tmp = dummy; //使用的是尾插入法 for (int i = 0; i<sizeof(array) / sizeof(int); i++) { ListNode *p = new ListNode(array[i]); tmp->next = p; tmp = tmp->next; } head = dummy->next; delete dummy; return head; } //显示链表 void showList(ListNode *head) { while (head) { printf("%d ", head->data); head = head->next; } printf("\n"); return; } //摧毁链表 void desroyList(ListNode *head) { while (head) { ListNode *tmp = head->next; delete head; head = tmp; } return; } void main() { Solution sol1; //创建一个空链表 ListNode *dummy = createList(); //对链表初始化,返回head ListNode *head = initList(dummy); //显示链表 showList(head); //swap head = sol1.reverseKGroup(head, 4); //显示链表 showList(head); system("pause"); desroyList(head); }
方法二:不利用栈结构,直接在原链表中调整
//不利用栈结构,在原链表中直接调整 class Solution{ public: ListNode* reverseKGroup(ListNode* head, int k) { if (head == NULL || head->next == NULL || k < 2) return head; ListNode *cur = head; ListNode *start = NULL; ListNode *pre = NULL; ListNode *next = NULL; int count = 1; while (cur != NULL) { next = cur->next; if (count == k) { start = pre == NULL ? head : pre->next; head = pre == NULL ? cur : head; resign2(pre, start, cur, next); pre = start; count = 0; } count++; cur = next; } return head; } void resign2(ListNode* left, ListNode* start, ListNode *end,ListNode* right){ ListNode *pre = start; ListNode* cur = start->next; ListNode*next = NULL; while (cur!=right) { next = cur->next; cur->next = pre; pre = cur; cur = next; } if (left != NULL){ left->next = end; } start->next = right; //上一组的最后一个结点(逆序前的第一个结点)链接下一组逆序前的第一个结点 } };
相关文章推荐
- nodejs
- vs code和node的相关使用 一一 typescript的配置
- 神之门V8(1):解密handle<T>
- nodejs-初探-简单的判定位置是否在市区API
- 从helloworld开始构建一个node.js+socket.io+express4实时应用示例
- 遇见NodeJS:JavaScript的贵人
- node js 常用模块
- node.js开发指南(书籍)
- node.js开发小项目的全过程 2 初识目录结构
- 关于本人遇到的nodejs的一些错误信息
- 拿什么守护你的Node.JS进程: Node出错崩溃了怎么办? foreverjs, 文摘随笔
- hadoop 完全分布式 下 datanode无法启动解决方法
- ionic安装
- windows下安装nodejs
- nodejs 改变全局前缀
- nodejs向客户端推送消息
- node+express中用async来避免sql嵌套
- NodeJs处理Get请求和Post请求
- UE4 编译后 不能正常使用Open Level 打开关卡解决方案:Open Level Blueprint Node not workin
- dojo的内容获取domNode