leetcode-algorithms-25 Reverse Nodes in k-Group
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-25 Reverse Nodes in k-Group的更多相关文章
- [Leetcode][Python]25: Reverse Nodes in k-Group
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 25: Reverse Nodes in k-Grouphttps://oj. ...
- [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 ...
- 【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 ...
- 【一天一道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 ...
- 【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 ...
- 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 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表)
Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表) 题目描述 已知一个链表,每次对k个节点进行反转,最后返回反转后的链表 测试样例 Inpu ...
- 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 ...
- 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)
[LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
- [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 ...
随机推荐
- .NET 介绍
In order to continue our effort of being modular and well factored we don’t just provide the entire ...
- 杭电hdu-6168 Numbers
这一题是考察排序与后续数据处理的题.我是用了map来给“和”做标记,把确定为a数组内数的数两两求和.给这些和标记,这样就可以很好的处理带有重复数的数据了~~ 贴个碼: #include<iost ...
- java中Properties类及读取properties中属性值
本文为博主原创,未经允许不得转载: 在项目的应用中,经常将一些配置放入properties文件中,在代码应用中读取properties文件,就需要专门的类Properties类,通过这个类可以进行读取 ...
- jQuery中的$(window).load()与$(document).ready()以及jquery $(document).ready() 与window.onload的区别
大多数jQuery实例或教程都告诉我们绑定我们的jQuery代码到$(document).ready事件.虽然$(document).ready 事件在大多数情况下都OK,但是它的解析顺序是在文档准备 ...
- 51nod 1672 区间交(贪心)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1672 题意: 思路:其实这就是一个经典的区间贪心问题,只需要按照左端点排 ...
- Spring-MVC依赖
<dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api& ...
- MyBatis数据库测试代码自动生成
<!-- generatorConfig.xml配置,其中:<plugin type="org.mybatis.generator.plugins.ToStringPlugin& ...
- mutect/mutsig/gistic官网汇总
http://software.broadinstitute.org/software/cprg/
- Java三种代理模式:静态代理、动态代理和cglib代理
一.代理模式介绍 代理模式是一种设计模式,提供了对目标对象额外的访问方式,即通过代理对象访问目标对象,这样可以在不修改原目标对象的前提下,提供额外的功能操作,扩展目标对象的功能. 简言之,代理模式就是 ...
- prometheus的agent 二次开发代码参考
import com.codahale.metrics.MetricRegistry;import io.prometheus.client.CollectorRegistry;import io.p ...