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: 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
思路:其实就是反转多个链表,然后把这多个链表连接起来即可.
code:
class Solution {
public:
ListNode *reverseKGroup(ListNode *head, int k) {
if(head==NULL||k<) return NULL;
if(k==||head->next==NULL) return head;
bool first=true; vector<ListNode*> lastOfGroup;int loc=;
ListNode* prep=NULL;
ListNode* p=NULL;
ListNode* move=head;
ListNode* nextHead=NULL;
ListNode* resHead=NULL;
bool firsttag=true; while(move!=NULL){
int i=;
nextHead=move;
while(i<=k&&move!=NULL){
move=move->next;
++i;
}
prep=nextHead;
p=nextHead->next;
lastOfGroup.push_back(prep);
if(i-==k){
prep->next=NULL;
int j=;
while(j<=k-){
ListNode* tempNextNode=p->next;
p->next=prep;
prep=p;
p=tempNextNode;
++j;
}
if(firsttag)
resHead=prep;
if(!firsttag)
lastOfGroup[loc++]->next=prep;
firsttag=false;
}else{
if(firsttag) return head;
lastOfGroup[loc++]->next=nextHead;
}
}
return resHead;
}
};
Reverse Nodes in k-Group (链表)的更多相关文章
- [Leetcode] Reverse nodes in k group 每k个一组反转链表
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...
- Reverse Nodes In K Group,将链表每k个元素为一组进行反转---特例Swap Nodes in Pairs,成对儿反转
问题描述:1->2->3->4,假设k=2进行反转,得到2->1->4->3:k=3进行反转,得到3->2->1->4 算法思想:基本操作就是链表 ...
- leetcode Reverse Nodes in k-Group翻转链表K个一组
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k ...
- 【Reverse Nodes in k-Group】cpp
题目: Given a linked list, reverse the nodes of a linked list k at a time and return its modified list ...
- [LintCode] Reverse Nodes in k-Group 每k个一组翻转链表
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...
- Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表)
Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表) 题目描述 已知一个链表,每次对k个节点进行反转,最后返回反转后的链表 测试样例 Inpu ...
- [LeetCode]25. Reverse Nodes in k-Group k个一组翻转链表
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k ...
- LeetCode 笔记系列六 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. ...
- LeetCode 25 Reverse Nodes in k-Group Add to List (划分list为k组)
题目链接: https://leetcode.com/problems/reverse-nodes-in-k-group/?tab=Description Problem :将一个有序list划分 ...
- LeetCode: Reverse Nodes in k-Group 解题报告
Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked list k at a time and ret ...
随机推荐
- Java长存!12个Java长久占居主要地位的原因
Java长存!12个Java长久占居主要地位的原因 我们很容易就会遗忘那些曾经在猿群中大热而又被各种新技术掩盖直至堙灭的技术的价值.就拿COBOL这个老猿们当年所用的神器来说,就跟条死鱼一样被现代猿基 ...
- 纯css滚动公告栏目
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- powerDesigner 一些设置
常用设置 table中需要显示的内容 --------------------------------------------------------------------------------- ...
- python struct.pack方法报错argument for 's' must be a bytes object 解决
参考 https://blog.csdn.net/weixin_38383877/article/details/81100192 在python3下使用struct模块代码 fileHead = s ...
- vue $parent 的上一级 有可能不是父组件,需要好几层$parent 如果这样 还不如用 this.$emit
vue $parent 的上一级 有可能不是父组件,需要好几层$parent 如果这样 还不如用 this.$emit
- python-水仙花数
>>> for a in range(1,10):... for b in range(0,10):... for c in range(0,10):... x=100*a+10*b ...
- MySQL存储过程实践
对employees数据库建立存储过程 创建不含有输入输出变量的存储过程 DELIMITER // -- 设定语句结束分隔符 DROP PROCEDURE IF EXISTS GetEmployees ...
- laravel知识点备忘
1.连表查询:select * from goods left join shop on goods.shopid=shop.shopid; DB::table('goods') ->leftJ ...
- jQuery的on绑定click和直接绑定click区别
状况之外 在之前的公司并没有遇到这个问题,也就没有深究.直到自己换了现在的公司,刚来第二天就开始写别人写到一半的项目,很无奈,不是原生就是jquery,由于项目急,已经来不及切换框架重新布局,只能继续 ...
- 前端基础之JavaScript_1
摘要: JavaScript简介 引入方式 语言规范 JavaScript语言基础 变量声明 数据类型 运算符 流程控制 函数 词法分析 内置对象 一.JavaScript概述 1.ECMAScrip ...