leetcode-algorithms-25 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.

k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.

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

Note:

Only constant extra memory is allowed.

You may not alter the values in the list's nodes, only nodes itself may be changed.

解法

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution
{
public:
ListNode* reverseKGroup(ListNode* head, int k)
{
ListNode d(-1);
d.next = head; int count = 0;
ListNode *pre_first = &(d);
ListNode *first = pre_first;
ListNode *second;
ListNode *second_last;
//count the list
while(first = first->next) count++;
while(count >= k)
{
first = pre_first->next;
second = first->next;
//reverse the K node
for (int i = 1; i < k; ++i)
{
second_last = second->next;
second->next = pre_first->next;
pre_first->next = second;
first->next = second_last;
second = second_last;
}
pre_first = first;
count -= k;
} return d.next;
}
};

时间复杂度: O(n + k*(n/k)),n是链表的长度.

空间复杂度: O(1).

链接: leetcode-algorithms 目录

leetcode-algorithms-25 Reverse Nodes in k-Group的更多相关文章

  1. [Leetcode][Python]25: Reverse Nodes in k-Group

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 25: Reverse Nodes in k-Grouphttps://oj. ...

  2. [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 ...

  3. 【LeetCode】25. Reverse Nodes in k-Group (2 solutions)

    Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked list k at a time and ret ...

  4. 【一天一道LeetCode】#25. Reverse Nodes in k-Group

    一天一道LeetCode系列 (一)题目 Given a linked list, reverse the nodes of a linked list k at a time and return ...

  5. 【LeetCode】25. 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. k  ...

  6. 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 算法思想:基本操作就是链表 ...

  7. Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表)

    Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表) 题目描述 已知一个链表,每次对k个节点进行反转,最后返回反转后的链表 测试样例 Inpu ...

  8. 24. Swap Nodes in Pairs(M);25. Reverse Nodes in k-Group(H)

    24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...

  9. 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)

    [LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

  10. [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  ...

随机推荐

  1. 在Vue的构造器里我们写一个add方法,然后我们用实例的方法调用它

    html <div id="app"> <div>{{message}}</div> </div> js var vm = new ...

  2. 剥开比原看代码11:比原是如何通过接口/create-account创建帐户的

    作者:freewind 比原项目仓库: Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchai ...

  3. facebook api之Business Manager API

    Business-scoped Users - The new user is tied to a particular business and has permissions scoped to ...

  4. SAP月结操作讲解

    SAP月结操作讲解 https://wenku.baidu.com/view/ac6fe45d312b3169a451a4b9.html   步聚 操作内容 事务码 是否必须 操作时间 月/年结 1 ...

  5. 每天一个小程序—0013题(爬图片+正则表达式 or BeautifulSoup)

    第 0013 题: 用 Python 写一个爬图片的程序,爬 这个链接里的日本妹子图片 :-) 关于python3的urllib模块,可以看这篇博客:传送门 首先是用urlopen打开网站并且获取网页 ...

  6. HDU 4318 Power transmission(最短路)

    http://acm.hdu.edu.cn/showproblem.php?pid=4318 题意: 给出运输路线,每条路线运输时都会损失一定百分比的量,给定起点.终点和初始运输量,问最后到达终点时最 ...

  7. BZOJ 2809: [Apio2012]dispatching(左偏树)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2809 题意: 思路:最简单的想法就是枚举管理者,在其子树中从薪水低的开始选起,但是每个节点都这样处理 ...

  8. QT使用QJson生成解析Json数据的方法

    QT中使用json还是比较方便的,下面用例子直接说明 举例子之前首先推荐一个在线解析json格式的网站,具体格式用法如下图所示: 之后根据这个格式进行json数据解析. QT使用json需要包含的头文 ...

  9. C#中引用第三方ocx控件引发的问题以及解决办法

    调用OCX控件的步骤:1.在系统中注册该ocx控件,命令:regsvr32.exe 控件位置(加 /u 参数是取消注册)2.在.net的工具箱中添加该控件,拖到form中去就可以了. 不用工具箱的话, ...

  10. IntelliJ IDEA Tomcat中端口被占用的问题

    早上来公司,新建了一个项目,启动Tomcat,报错,如图所示 端口1099被占用 cmd——netstat -aon|findstr 1099 此时出现了一个问题,输入:netstat -an,提示: ...