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 ...
随机推荐
- centos启用socks5服务
直接在终端用 root 安装 *** 官方客户端 apt-get install python-pip -ypip install shadowsocks 然后编辑 /etc/shadowsocks. ...
- sql server 2000备份还原数据库
转载请注明出处:http://blog.csdn.net/neochan1108/article/details/79248017 备份: -- Create the backup device fo ...
- 语音行业技术领先者Nuance诚招ASR/NLP研发工程师和软件工程师
Nuance is a leading provider of voice and language solutions for businesses and consumers around the ...
- js 数组过滤 filter
let res = this.list.filter(item => routeEqual(this.currentRouteObj, item) || item.name === this.$ ...
- vscode 快捷键 ctrl+shift+F 冲突了 解决办法
vscode 快捷键 ctrl+shift+F 冲突了 解决办法 1.修复 搜狗输入法 ctrl+shift+F 中文 繁体简体的快捷键冲突 2.修复 微软输入法 ctrl+shift+F 冲突 ( ...
- 使用Maven构建JavaEE项目
学习要点 Maven简介 Maven构建项目 MyEclipse中Maven的使用 Maven简介 Maven作用 对第三方依赖库进行统一的版本管理 统一的目录结构,统一各平台各IDE目录 统一的软件 ...
- jquery 拖动(Draggable) 约束运动,输出数组排序Array
<!doctype html><html lang="en"><head> <meta charset="utf-8" ...
- [题解] cogs 2240 架设电话线路
http://cogs.pro:8080/cogs/problem/problem.php?pid=2240 与洛谷P2885几乎一致,https://www.luogu.org/problemnew ...
- codevs 2853 方格游戏--棋盘dp
方格游戏:http://codevs.cn/problem/2853/ 这和传纸条和noip方格取数这两个题有一定的相似性,当第一眼看到的时候我们就会想到设计$dp[i][j][k][l]$(i,j表 ...
- python 03 8/25-8/27 range 、randint
import random """字符串的操作中 三种方法,只包含左索引,不包含右索引""" hi= "bokeyuan pyth ...