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 ...
随机推荐
- sublime text3前端开发插件配置以及使用(个人喜爱)
第一步下载软件接着Ctrl +~ (回车)把下面安装包管理添加到sublimeimport urllib.request,os; pf = 'Package Control.sublime-packa ...
- Android Studio 中文件查询方法总结
搜索单词 Windows: Ctrl + F Mac : Cmd + F 会在当前激活的文件上查询输入的关键字,以高亮显示 跳转行 Windows: Ctrl + L Mac : Cmd + ...
- Python3简明教程(五)—— 流程控制之循环
有些时候我们需要多次执行相同的任务,我们使用一个计数器来检查代码需要执行的次数.这个技术被称为循环. while循环 while语句的语法如下: while condition: statement1 ...
- 技术抄录_Java高级架构师教程
1.B2C商城项目实战 2.高性能架构专题 3.架构筑基与开源框架解析专题 4.团队协作开发专题 5.微服务架构专题 6.设计模式 附上[架构资料] ...
- 爬虫学习之csv读取和存储
一.读取 该读取主要使用到csv里面的Reader().DictReader()方法,和引用io里面的StringIO进行对字符串进行封装 在处理网上的csv文件方式主要是有一下几方面: • 手动把C ...
- css--使用的四种方法
前戏 之前学习了HTML相关的知识,也能简单的写一个hello world的页面.但是,只学HTML满足不了我们的需求,而HTML.CSS.JavaScript三者搭配使用才能更好的完成我们需要的效果 ...
- Redis进阶例子
工作中用到的RabbitMQ例子 , 但是最后没有用 , 用的CMQ , 顺便说下CMQ社区真的少 , 并且功能少 . 一.消息体 package com.bootdo.common.rabbitmq ...
- Java会话(session)管理
会话概述 什么是会话 简单的理解:用户打开浏览器,点击多个超链接,访问Web服务器上多个资源,然后关闭浏览器,整个过程称之为一次会话. 需要解决的问题 每个用户在使用浏览器与服务器会话的过程中,会产生 ...
- 【Linux】Centos6的iptables防火墙设置
1,查看防火墙状态 # service iptables status //或 # /etc/init.d/iptables status 2,防火墙的启动.重启,关闭 # service iptab ...
- root连接ubuntu18.04“拒绝访问”的解决方法
1.设置root账户 sudo passwd root 2.ssh远程登陆拒绝访问:修改SSH配置文件 sudo vim /etc/ssh/sshd_config 找到并用#注释掉这行:PermitR ...