1、题目描述

2、问题分析

这个题本质上还是按照链表翻转的思路来解,只是需要添加一些细节判断。

3、代码

 class Solution {
public:
ListNode* reverseKGroup(ListNode* head, int k) {
if( !head || head->next == NULL || k <= ){
return head ;
} ListNode dummy() ;
ListNode* d = &dummy ;
ListNode* p = head ; while( p != NULL ){
ListNode* pm = p;
int n = ;
ListNode* nextNode ;
while( pm != NULL && n < k ){
pm = pm->next ;
++n;
}
if( pm != NULL ){
nextNode = pm->next;
pm->next = NULL ; d->next = reverseList( p );
d = p;
p = nextNode ; // k % number of nodes == 0 ,out of while ; }else{
d->next = p;
break;
} } return dummy.next ; } ListNode* reverseList( ListNode* head ){
if( !head || head->next == NULL ){
return head;
}
ListNode* p = head ;
ListNode* q = head->next ;
head->next = NULL ;
while( p != NULL && q != NULL ){
ListNode* tmp = q->next ;
q->next = p;
p = q ;
q = tmp ;
}
return p ; }
};

LeetNode 题解之Reverse Nodes in k-Group的更多相关文章

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. Android之密码的显示与隐藏

    很多应用都是显示与隐藏密码的功能. 之前的项目都没这个功能要求,也没有专门研究这个.最近项目有加这个功能,我这里也刚好整理一下. 我的思路是设置EditText的InputType.代码如下: if ...

  2. Activity四大启动模式

    ctivity的四种启动模式: standard.singleTop.singleTask.singleInstance 为了打印方便,定义一个基础Activity,在其onCreate方法和onNe ...

  3. C/C++求职宝典21个重点笔记(常考笔试面试点)

    这是我之前准备找工作时看<C/C++求职宝典>一书做的笔记,都是一些笔试面试中常考的重点难点问题,但比较基础,适合初学者看. 1. char c = '\72'; 中的\72代表一个字符, ...

  4. 【详解】ThreadPoolExecutor源码阅读(二)

    系列目录 [详解]ThreadPoolExecutor源码阅读(一) [详解]ThreadPoolExecutor源码阅读(二) [详解]ThreadPoolExecutor源码阅读(三) AQS在W ...

  5. Linux QT 连接 Sqlite数据库

    #include <QApplication> #include <QDebug> #include <QSqlQuery> #include <QSqlDa ...

  6. Log4j的邮件发送类SMTPAppender改造

    在开发过程中,我们有时需要将重要的错误日志通过邮件发送给相关的责任人,这样能即时发现错误,即时解决.如使用Log4J,一般会做如下配置: log4j.rootLogger = debug,mail # ...

  7. UVa 10129 Play on Words(并查集+欧拉路径)

    题目链接: https://cn.vjudge.net/problem/UVA-10129 Some of the secret doors contain a very interesting wo ...

  8. [NOI 2016]优秀的拆分

    Description 题库链接 给你一个长度为 \(n\) 的只含小写字母的字符串 \(S\) ,计算其子串有多少优秀的拆分. 如果一个字符串能被表示成 \(AABB\) 的形式,其中 \(A,B\ ...

  9. 并发编程之 线程协作工具 LockSupport

    前言 在前面的文章中,我们介绍了并发工具中的4个,Samephore,CyclicBarrier,CountDownLatch,Exchanger,但是我们漏了一个,非常的好用的工具,楼主在这里必须加 ...

  10. Centos7.X通过rpm包安装Docker

    目录 前言 1.Docker官网下载rpm包 2.通过liunx命令安装rpm包 3.迁移镜像存储路径 前言 Docker已经火了很多年,现在各大公司都会使用它.那么在我们日常开发中也经常使用,比如我 ...