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. 最大流之dinic

    先用bfs预处理出层次图,然后在层次图上用dfs找增广路径,理论复杂度O(n*n*m) const int INF=0xfffffff ; struct node { int s,t,cap,nxt ...

  2. rancher下的kubernetes之二:安装rancher和kubernetes

    在上一章<rancher下的kubernetes之一:构建标准化vmware镜像>,我们做了个通用的虚拟机镜像,可以root登录,apt已经更新,docker也装好了,现在我们就来安装ra ...

  3. python3 scrapy 使用selenium 模拟浏览器操作

    零. 在用scrapy爬取数据中,有写是通过js返回的数据,如果我们每个都要获取,那就会相当麻烦,而且查看源码也看不到数据的,所以能不能像浏览器一样去操作他呢? 所以有了-> Selenium ...

  4. postman tests实例记录(还没看,一些常用的)

    这段时间准备测试api接口,postman这个工具很是方便,特别是里面的tests的javascript脚本. 记录一下测试接口常用的tests验证的实例. 1.设置环境变量 postman.setE ...

  5. postman 前置 和 后置 处理器 用法

    基本用法 赋予变量 var  body="我是变量的值" ;   -----给body赋值 postman.setEnvironmentVariable("sign&qu ...

  6. Train-Alypay-Cloud:mPaaS 移动开发平台培训(第一次)

    ylbtech-Train-Alypay-Cloud:mPaaS 移动开发平台培训(第一次) 1.返回顶部 1. 大家好! 欢迎大家参加蚂蚁金融云 即将在2018年1月17日到1月18日 在北京 环球 ...

  7. [Android] 开发第十天

    这几天因为电脑的 USB口发生故障,一直没怎么玩 Android-Studio 后来把电脑从  Win7 -> Win10 重装后,一部分 USB口 可以使用了,然后接着开发 Android 接 ...

  8. java之扫描包里面的class文件

    一.class作为,编译过后的产物,在很多时候,我们需要通过反射去执行class的具体方法.但是扫描class就是一个很大的问题了. 二.所以我这里写了一个简单的class文件扫描方式. 三.主要是利 ...

  9. Microsoft SQL Server on Linux破解 2G内存限制

    首先,贴上微软官方安装方法,大家按照官方的操作就行. 微软官方安装方法 相信很多同学遇到一个问题就是: sqlservr: This program requires a machine with a ...

  10. Vue 安装环境创建项目

    vue 是一个单页面框架,基于模块化组件化的开发模式. 搭建开发环境之前必须要安装node.js,然后安装vue的脚手架工具(命令行工具)win + R 输入npm install  --global ...