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 ...
随机推荐
- bat判断服务是否启动
sc query|find "tomcat6" && echo yes || echo nosc query|find "eventlog" & ...
- 原生js的容易忽略的相似点(一)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- vue利用计算属性做(展开收起)小例子
<template> <div class="wrap"> <div class="box"> <div v-for= ...
- 获取当前目录 文件输出html 网页查看
@echo off setlocal set LISTFILE=list.html echo MAKING LISTFILE … (PLEASE WAIT) echo ^<!doctype ht ...
- HDU 5391 Zball in Tina Town (打表,水)
题意: Tina有一个球,它的名字叫zball.zball很神奇,它会每天变大.在第一天的时候,它会变大1倍.在第二天的时候,它会变大2倍.在第n天的时候,它会变大n倍.zball原来的体积是1.Ti ...
- python之路——函数进阶
阅读目录 楔子 命名空间和作用域 函数嵌套及作用域链 函数名的本质 闭包 本章小结 楔子 假如有一个函数,实现返回两个数中的较大值: def my_max(x,y): m = x if x> ...
- IntelliJ IDEA集成工具Database连接MySQL8.0报错的解决方法
直接用默认配置连接的话,会报以下错误: Connection to MySQL - @localhost failed. [08001] Could not create connection to ...
- xmind 8 安装后启动失败(未提示错误信息)
xmind 8 安装后启动失败 前言 家里的计算机也安装了xmind,启动之后界面显示xmind的启动图标,几秒之后启动图标消失(闪退了),然后留我一脸懵逼.想着卸载了安装一个新的应该没有问题 ...
- RabbitMQ 关键词解释
源地址: https://www.cnblogs.com/hz04022016/p/6518138.html RabbitMQ是流行的开源消息队列系统,用erlang语言开发.RabbitMQ是AMQ ...
- 【HIHOCODER 1039】 字符消除
链接 问题描述 小Hi最近在玩一个字符消除游戏.给定一个只包含大写字母"ABC"的字符串s,消除过程是如下进行的: 1)如果s包含长度超过1的由相同字母组成的子串,那么这些子串会被 ...