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

思路:注意最后不满k个swap,需要再做一次reverse,使之成为正序。

指针赋值时注意前一个右项作为后一个左向,一一赋值。

/**
* 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* dummyHead = new ListNode();
dummyHead->next = head;
ListNode* current = head; //the node to do swap
ListNode* subHead = dummyHead; //the node before the head of the group
ListNode* subTail; //the last node of the group while(current && current->next){
subTail = current;
current = current->next;
for(int i = ; i < k; i++){ //k group needs k-1 swap
if(current==NULL){
//reset: do once more reverse
subTail = subHead->next;
current = subTail->next;
for(int j = ; j < i; j++){
subTail->next = current->next;
current->next = subHead->next;
subHead->next = current;
current = subTail->next;
}
break;
} //assign value one by one
subTail->next = current->next;
current->next = subHead->next;
subHead->next = current;
current = subTail->next;
}
subHead = subTail;
} return dummyHead->next;
}
};

25.Reverse Nodes in k-Group (List)的更多相关文章

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

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

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

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

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

  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. 25. Reverse Nodes in k-Group (JAVA)

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

  8. 蜗牛慢慢爬 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. ...

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

随机推荐

  1. react native遇到的坑

    1.模拟器报错no bundle url present https://github.com/facebook/react-native/issues/12754 http://www.cnblog ...

  2. PostgreSQL truncate table会释放索引的空间

    apple=# create table test(id integer, info text); CREATE TABLE apple=# insert into test select gener ...

  3. 第十课 C++异常简介

    异常不是错误,异常是程序中可预料到的另一条执行分支,是可预见的.错误是不可预料的. C++内置了异常处理的语法元素try...catch...,如下: C++通过throw语句抛出异常信息: 上图中t ...

  4. 建立dblink(database link)

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/bisal/article/details/26730993 database linke是建立一个数 ...

  5. nyoj A+B Problem IV

    A+B Problem IV 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 acmj最近发现在使用计算器计算高精度的大数加法时很不方便,于是他想着能不能写个程序把这 ...

  6. Spring AOP 实现读写分离

    原文地址:Spring AOP 实现读写分离 博客地址:http://www.extlight.com 一.前言 上一篇<MySQL 实现主从复制> 文章中介绍了 MySQL 主从复制的搭 ...

  7. Remi 安装源

    Remi repository 是包含最新版本 PHP 和 MySQL 包的 Linux 源,由 Remi 提供维护.有个这个源之后,使用 YUM 安装或更新 PHP.MySQL.phpMyAdmin ...

  8. windows server 2012 双网卡配置

    别用route 命令!!!!!! 在使用最新版的windows server 2012的时候,当存在两个或者多个网段的时候,就可以采用双网卡的方式来添加和配置路由.具体的设置方法如下: 网段1  19 ...

  9. vs2015 去除 git 源代码 绑定

    第一次碰到这个问题,想将源代码签入TFS管理.添加到源码管理后,默认添加到Git源码管理. 研究过后,发现: 1)删除框内文件 2)Vs2015->工具->选项->源代码管理-> ...

  10. C++ 数据的封装 初始封装

    C++ 数据封装 所有的 C++ 程序都有以下两个基本要素: 程序语句(代码):这是程序中执行动作的部分,它们被称为函数. 程序数据:数据是程序的信息,会受到程序函数的影响. 封装是面向对象编程中的把 ...