class Solution {
public:
ListNode *reverseKGroup(ListNode *head, int k) {
if (!head || !(head->next) || k < 2)
return head; // count k nodes
ListNode *nextgp = head;
for (int i = 0; i < k; i++)
if (nextgp)
nextgp = nextgp->next;
else
return head; // reverse
ListNode *prev = NULL, *cur = head, *next = NULL;
while (cur != nextgp) {
next = cur->next;
if (prev)
cur->next = prev;
else
cur->next = reverseKGroup(nextgp, k);
prev = cur;
cur = next;
}
return prev;
}
};

  

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k is a positive integer and is less than or equal to the length of the linked list. If the number of的更多相关文章

  1. 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.

    我用String代替了链表显示,本题的大意是每k个进行逆序处理,剩下的不够k个的就按照原顺序保留下来. public class ReverseNodes { public static void m ...

  2. [Linked List]Reverse Nodes in k-Group

    Total Accepted: 48614 Total Submissions: 185356 Difficulty: Hard Given a linked list, reverse the no ...

  3. 【LeetCode】9 & 234 & 206 - Palindrome Number & Palindrome Linked List & Reverse Linked List

    9 - Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. Som ...

  4. HIVE出现Read past end of RLE integer from compressed stream Stream for column 1 kind LENGTH position: 359 length: 359 range: 0错误

    错误日志 Diagnostic Messages for this Task: Error: java.io.IOException: java.io.IOException: java.io.EOF ...

  5. [Linked List]Reverse Linked List,Reverse Linked List II

    一.Reverse Linked List  (M) Reverse Linked List II (M) Binary Tree Upside Down (E) Palindrome Linked ...

  6. LeetCode之“链表”:Reverse Linked List && Reverse Linked List II

    1. Reverse Linked List 题目链接 题目要求: Reverse a singly linked list. Hint: A linked list can be reversed ...

  7. *Amazon problem: 234. Palindrome Linked List (reverse the linked list with n time)

    Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...

  8. Data Structure Linked List: Reverse a Linked List in groups of given size

    http://www.geeksforgeeks.org/reverse-a-list-in-groups-of-given-size/ #include <iostream> #incl ...

  9. 863. All Nodes Distance K in Binary Tree 到制定节点距离为k的节点

    [抄题]: We are given a binary tree (with root node root), a target node, and an integer value K. Retur ...

随机推荐

  1. GIT GUI克隆github代码

    新建一个文件夹,右击gitgui  git clone 去掉不要

  2. mirror.js 整合redux的好工具

    mirror.js 很简单,让state管理更方便了,没有新增api,值 得使用 https://github.com/yurizhang/mirror package.json { "na ...

  3. 【持续更新】JS 时间与日期

    JS 的日期时间在项目中是必定会用到的,所以必须掌握. UTC 与 GMT 背景 十七世纪,格林威治皇家天文台为了海上霸权的扩张计画而进行天体观测.1675年旧皇家观测所(Old Royal Obse ...

  4. 十四个关于ASP.NET基础知识问答(C#版)

    这是一些ASP.NET很基础的东西,希望对ASP.NET爱好者特别是刚刚入门的朋友有所帮助虽然示例代码是C#.NET,但是不影响VB.NET朋友的参考.好,继续往下看吧! 1.ASP.NET能在那些系 ...

  5. IIS 服务器支持下载apk 文件

    前不久,在本地IIS文件下部署一个网站,可以下载apk文件,就是测试apk应用升级,发现访问不能下载,原因是IIS没有配置对这种apk文件的处理程序. 解决方案如下所示: 1.打开IIS, 找到MIM ...

  6. 警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} 解决方法

    Tomcat启动时出现红色警告内容 警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'sour ...

  7. LeetCode Add and Search Word - Data structure design (trie树)

    题意:实现添加单词和查找单词的作用,即实现字典功能. 思路:'.' 可以代表一个任何小写字母,可能是".abc"或者"a.bc"或者"abc.&quo ...

  8. 通过90行代码学会HTML5 WebSQL的4种基本操作

    Web SQL数据库API是一个独立的规范,在浏览器层面提供了本地对结构化数据的存储,已经被很多现代浏览器支持了. 我们通过一个简单的例子来了解下如何使用Web SQL API在浏览器端创建数据库表并 ...

  9. decompressedResponseImageOfSize:completionHandler:]_block_invoke

    原因:   It turns out the linker error was caused by the CGImageSourceCreateWithData call. And the root ...

  10. eclipse 的project explorer问题,这个怎样把localFileSystem去掉,

    这个非常简单 把勾去掉就可以了