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 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)的更多相关文章
- [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 ...
- 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 ...
- [Leetcode][Python]25: Reverse Nodes in k-Group
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 25: Reverse Nodes in k-Grouphttps://oj. ...
- 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 25 Reverse Nodes in k-Group Add to List (划分list为k组)
题目链接: https://leetcode.com/problems/reverse-nodes-in-k-group/?tab=Description Problem :将一个有序list划分 ...
- 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 ...
- 蜗牛慢慢爬 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. ...
- 【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 ...
随机推荐
- Java 反射机制介绍
参考文章:http://www.cnblogs.com/skywang12345/p/3345205.html Java 反射机制.通俗来讲呢,就是在运行状态中,我们可以根据“类的部分已经的信息”来还 ...
- C#统计给定的文本中字符出现的次数,使用循环和递归两种方法
前几天看了一个.net程序员面试题目,题目是”统计给定的文本中字符出现的次数,使用循环和递归两种方法“. 下面是我对这个题目的解法: 1.使用循环: /// <summary> /// 使 ...
- udev学习笔记汇总
1.什么是udev udev--就是动态设备管理 udev 能够处理设备事件.管理设备文件的权限.在/dev目录中创建额外的符号链接.重命名网络接口,等等. 内核通常仅根据设备被发现的先后顺序给设备文 ...
- fb远程连接服务器调试,碉堡了
开发中经常碰到本地代码没问题,上传到服务器上就有有问题, 这个时候调试变的很麻烦,放个textField自己保存日志这种方式调试的都是. 今天刚学了远程连接服务器,adobe真是牛逼坏了啊. 新增一个 ...
- 【转】linux sed命令详解
原文网址:http://www.iteye.com/topic/587673 1. Sed简介sed 是一种在线编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”( ...
- js基础篇string&&array(应YX同学面试复习要求 - -)
js中的数据类型一共有五个基本数据类型,分别是undefined,null,boolean,number,string. js中的Object类型中包括两大类型:Function类型和array类型. ...
- emacs之配置3,键盘和鼠标设置
emacsConfig/kbd-mouse-setting.el ;;强制TAB键使用空格 (setq-default indent-tabs-mode nil) ;M-i执行tab-to-tab-s ...
- log4net 使用指南,最常遇到的问题整理。。。
一. Log4net特征 Log4net是一个用于.NET开发环境的日志记录组件,由于它的超快及超灵活,很多大型的应用都会用到. 它有如下特点: 1.自定义日志输出级别 ...
- .net 提取注释生成API文档 帮助文档
提取注释生成API文档 一.前言 在多人协作的项目中,除了良好的代码规范外,完整的API文档也相当重要.通过文档我们快速了解系统各模块的实际接口,及其使用场景.使用示例,一定程度上降低沟通成本,和 ...
- expect学习笔记及用法
expect学习笔记及实例详解 expect的基本用法 expect用法