LeetCode25 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 (Hard)
分析:
题目意思是每k个链表进行翻转,不足k个保持不变。
可以写一个翻转k个节点的辅助函数,然后在原函数中判断后续是否有k个节点,以及后续节点位置。
注意事项见代码注释。
代码:
class Solution {
private:
ListNode* reverseKelement(ListNode* head, int k) {
ListNode* result = nullptr;
for (int i = ; i < k; ++i) {
ListNode* temp = head -> next;
head -> next = result;
result = head;
head = temp;
}
return result;
}
public:
ListNode* reverseKGroup(ListNode* head, int k) {
if (head == nullptr) {
return head;
}
ListNode dummy();
dummy.next = head;
head = &dummy;
ListNode* runner = head -> next;
int flag = ;
for (int i = ; i < k; ++i) {
if (runner -> next != nullptr || (runner -> next == nullptr && k == i + ) ) { //恰好k个到结尾情况没考虑, 例如[1,2] 2情况
runner = runner -> next;
}
else {
flag = ;
break;
}
}
while (flag == ) {
ListNode* temp1 = head -> next;
head -> next = reverseKelement(temp1, k);
temp1 -> next = runner;
head = temp1;
for (int i = ; i < k; ++i) {
if (runner != nullptr && (runner -> next != nullptr || (runner -> next == nullptr && k == i + )) ) { //对应添加runner != nullptr
runner = runner -> next;
}
else {
flag = ;
break;
}
}
}
return dummy.next;
}
};
LeetCode25 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 算法思想:基本操作就是链表 ...
- 【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 Add to List (划分list为k组)
题目链接: https://leetcode.com/problems/reverse-nodes-in-k-group/?tab=Description Problem :将一个有序list划分 ...
- 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: 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 ...
- 25.Reverse Nodes in k-Group (List)
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...
随机推荐
- Context3D 不可用
打开项目文件夹中的html-template,并找到index.template.html,右键使用TextEditor编辑,在params.allowfullscreen=”true”:后面加上pa ...
- dom div移动解决停顿问题
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- JavaScript 链式结构序列化详解
一.概述 在JavaScript中,链式模式代码,太多太多,如下: if_else: if(...){ //TODO }else if(...){ //TODO }else{ //TODO } swi ...
- 删除Ngnix 日志
删除Ngnix日志的脚本 #!/bin/bash #初始化 LOGS_PATH=$(pwd)/logs YESTERDAY=$(date -d "yesterday" +%Y-%m ...
- Codeforces Round #368 (Div. 2) A. Brain's Photos (水题)
Brain's Photos 题目链接: http://codeforces.com/contest/707/problem/A Description Small, but very brave, ...
- Ubuntu下Android编译环境的配置
从安装操作系统到编译程序结束,过程大致如下. 1. Ubuntu Linux操作系统安装软件包.使用 Ubuntu 14.04 Desktop系统.安装Linux系统到VMWare虚拟机上. 2. 完 ...
- 创建一个EMS 扩展包
EMS Package 向导: File > New > Other > Delphi projects > EMS > EMS Package Empty packag ...
- ASP.NET MVC- ActionFilter的使用
ActionFilter是穿插在Action执行过程,在Action执行前后提供扩展的功能.ActionFilter用途非常的广,用在页面压缩.缓存.错误处理,登陆验证. ActionFilter的实 ...
- C:结构体
结构体 构造类型:就是有基本的类型组成的 1.结构体 结构体是一种自定义的数据类型 和 int float 是一样的都可以定义变量 数组 只能存放一种类型的容器 结构体 可以存放多种数据类型 ...
- MES总结:CBF.Common 文件Net下的有类型转换
MES总结:CBF.Common 文件Net下的有类型转换. using System.Text;using System.Data;using System.ComponentModel;using ...