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 ...
随机推荐
- 怎么从sqlserver 数据库导出 insert 的数据语句
In SSMS in the Object Explorer, right click on the database right-click and pick "Tasks" a ...
- reds pub/sub官方文档翻译
Publish / Subscribe发布/订阅 redis-py includes a PubSub object that subscribes to channels and listens f ...
- java.lang.IllegalStateException: Cannot call sendError() after the response has been committed
http://blog.csdn.net/chenghui0317/article/details/9531171 —————————————————————————————————————————— ...
- shell's glob
[shell's glob] basic glob example: range glob example: 参考: http://bash.cumulonim.biz/glob.html
- Java断言assert
public class Welcome{ public static void main(String[] args){ assert false; System.out.println(" ...
- Python下调用Linux的Shell命令
有时候难免需要直接调用Shell命令来完成一些比较简单的操作,比如mount一个文件系统之类的.那么我们使用Python如何调用Linux的Shell命令?下面来介绍几种常用的方法: 1. os 模块 ...
- POJ3630Phone List(字典树)
经典的字典树的题目了,这次完全是按照自己的风格来写的,没有参考其他人的代码风格来写. 分析:如果采用常规的暴力枚举,那么复杂度就是O(n*n*str.length) = O(10^9),这明显是会超时 ...
- HDU 5776 sum (前缀和)
题意:给定 n 个数,和 m,问你是不是存在连续的数和是m的倍数. 析:考虑前缀和,如果有两个前缀和取模m相等,那么就是相等的,一定要注意,如果取模为0,就是真的,不要忘记了,我当时就没记得.... ...
- c#学习之旅------01
一.交换两个数的值 //交换两个数的值 #region 方法一 , num2 = ;//待交换的两个数值 int temp;//临时变量 temp = num1; num1 = num2; num2 ...
- Quartz.NET 2.0 学习笔记(1) :Quartz.NET简介
http://www.cnblogs.com/lzrabbit/archive/2012/04/13/2447609.html