前言

 

【LeetCode 题解】系列传送门:  http://www.cnblogs.com/double-win/category/573499.html

 

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.

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

2. 题意

给定一个链表,将链表按照顺序分成k元素的组,并将每个组中的元素倒置。

注意: 当小组中的元素不满k个时,不进行倒置操作。

3. 思路

可以利用插入法进行链表倒置,采用递归方法。

(1)链表倒置,可以参考文章:

(2)将链表分成k元素小组。

设置一个指针pre,指向插入点。

当插入的元素满足k个后,进行递归。

例如:链表为 1->2->3->4->5  k=2

可以将小组分为 1->2 3->4 5

for i range from 1 to k

tmp <- head

#将tmp插入到pre之后

head = head->next

(3)递归

上述步骤完成时, head->val = 3,进入到下一次处理

(4)终止条件, 剩余的组中元素不足k个,将该组直接返回,不做倒置。

4: 解法

class Solution {
public:
int getListLen(ListNode *head){
int len=0;
ListNode *tmp=head;
while(tmp){len++;tmp=tmp->next;}
return len;
}
ListNode *reverseKGroup(ListNode *head, int k) {
if(k<=1|| !head || !head->next) return head;
int len=getListLen(head);
if(len<k) return head; ListNode *pre=new ListNode(0);
ListNode *change=head; for(int j=1;j<=k;j++){
ListNode *tmp = change;
change = change->next;
tmp->next = pre->next;
pre->next=tmp;
}
head->next=reverseKGroup(change,k);
return pre->next;
}
};

作者:Double_Win

出处: http://www.cnblogs.com/double-win/p/3896010.html 

声明: 由于本人水平有限,文章在表述和代码方面如有不妥之处,欢迎批评指正~

[LeetCode 题解]: Reverse Nodes in K-Groups的更多相关文章

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

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

  2. 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划分 ...

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

  4. 【LeetCode题解】347_前K个高频元素(Top-K-Frequent-Elements)

    目录 描述 解法一:排序算法(不满足时间复杂度要求) Java 实现 Python 实现 复杂度分析 解法二:最小堆 思路 Java 实现 Python 实现 复杂度分析 解法三:桶排序(bucket ...

  5. 【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 ...

  6. 蜗牛慢慢爬 LeetCode 25. Reverse Nodes in k-Group [Difficulty: Hard]

    题目 Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. ...

  7. LeetCode 025 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 an ...

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

  9. [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. Hibernate SQL 查询

    本文转载自:https://www.cnblogs.com/li3807/p/6358386.html Hibernate 支持使用原生的SQL查询,使用原生SQL查询可以利用某些数据库特性,原生SQ ...

  2. DbUtils: JDBC Utility Component Examples

    DbUtils: JDBC Utility Component Examples \JDBCCollector\jdbc\src\main\java\com\ai\toptea\collection\ ...

  3. HDU6128-Inverse of sum

    参考这篇博客:https://blog.csdn.net/dormousenone/article/details/77340852 #include<bits/stdc++.h> usi ...

  4. CentOS 上使用vscode 调试百度大数据分析框架Apache Doris BE

    A: 前期准备工作 1. 安装vscode,详细请参见vscode官网https://code.visualstudio.com/docs/setup/linux,摘要如下: sudo rpm --i ...

  5. python实现进度条--主要用在上传下载文件

    在python中进行socket上传文件的时候使用进度条,然后在网上找了好久,找寻相关的进度的条的使用,看了几个,发现总是无法进行调用,主要原因是在进行上传文件的时候,每次传送的数据量是固定的,数据的 ...

  6. role是一个HTML5的属性

    <form role="form"> role是一个HTML5的属性,role="form"告诉辅助设备(如屏幕阅读器)这个元素所扮演的角色是个表单 ...

  7. zookeeper分布式锁和服务优化配置

    转自:https://www.jianshu.com/p/02eeaee4357f?utm_campaign=maleskine&utm_content=note&utm_medium ...

  8. LUA表与函数的深入理解

    local heroInfo = {} --直接打印 table的名字,就会输出该table的内存地址 print("表地址---------------",heroInfo) - ...

  9. 【316】python.requests 读取网页信息

    参考:Python:在网页中查找字符串的一般方法--in 参考:python怎么安装requests 参考:Requests 快速上手 操作步骤如下: 添加环境变量,将 python 所在文件夹添加至 ...

  10. rook issues

    ceph-volumeattacher: failed rbd single_major check, assuming it's unsupported: failed to check for r ...