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 ...
随机推荐
- SQL Server2012 T-SQL对分页的增强尝试
简介 SQL Server 2012中在Order By子句之后新增了OFFSET和FETCH子句来限制输出的行数从而达到了分页效果.相比较SQL Server 2005/2008的ROW_Numbe ...
- 伪类的格式重点:父标签层级 & 当前标签类型
伪类的格式重点:父标签层级 & 当前标签类型 通过例子说明: css1: span:nth-of-type(2){color: red;} css2: span :nth-of-type(2) ...
- (转)Spring4.2.5+Hibernate4.3.11组合开发
http://blog.csdn.net/yerenyuan_pku/article/details/52887573 搭建和配置Spring与Hibernate整合的环境 今天我们来学习Spring ...
- Metinfo 5.3.19管理员密码重置漏洞复现
Metinfo 5.3.19管理员密码重置漏洞 操作系统:Windows 10专业版 kali linux 网站环境:UPUPW 5.3 使用工具:burpsuite 1.7 beta 漏洞分 ...
- chrome 打开上次关闭的tab ctrl+shift+T
chrome 打开上次关闭的tab ctrl+shift+T
- “xxxx”表 - 无法修改表。 不能将值 NULL 插入列 'xxxx'
问题 向已有表增加字段 执行下面sql,sql执行增加两个字段分别: articleTitle 正标题 [nvarchar](200) articleSubTitle 副标题 [nvarchar](2 ...
- Java 对象的创建以及类加载
1. 对象的创建的过程: 类加载检查—>分配内存—>初始化零值—>设置对象头—>执行 init . 1.类加载检查: 虚拟机遇到一条 new 指令时,首先将去检查这个指令的参数 ...
- 如何使用MySQL一个表中的字段更新另一个表中字段
[本文出自:https://www.jb51.net/article/150323.htm] 这篇文章主要介绍了如何使用MySQL一个表中的字段更新另一个表中字段,需要的朋友可以参考下 1,修改1列 ...
- linux 部署nginx作为反向代理入口的内核参数/etc/sysctl.conf
# Kernel sysctl configuration file for Red Hat Linux## For binary values, 0 is disabled, 1 is enable ...
- Java:清空文件内容
文章来源:https://www.cnblogs.com/hello-tl/p/9139432.html import java.io.*; public class FileBasicOperati ...