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 ...
随机推荐
- 201621123010 《Java程序设计》第1周学习总结
1. 本周学习总结 本周主要学习了五个知识点 Java是面向对象的语言 JDK,JRE,JVM的联系 javac及java指令 Java跨平台运行的原理 新概念:类,类是面向对象中的概念 2. 书面作 ...
- Portability Flaw Locale Dependent Comparison
Portability Flaw Locale Dependent Comparison [问题描述] 该问题涉及String的toUpperCase()方法.具体通过例子演示相关现象. public ...
- Git Error: warning: refname 'origin/branch-name' is ambiguous.
When this happened, it created the file .git/refs/heads/origin/branch-name. So, I just deleted the f ...
- JMeter VS LoadRunner
对比项 JMeter LoadRunner 安装 简单,下载解压即可 复杂,安装包大于1GB,安装时间较久 录制/回放模式 支持 支持 测试协议 偏少,但用户可自行拓展 较多,用户不可自行拓展 分布式 ...
- LightOJ - 1205:Palindromic Numbers (数位DP&回文串)
A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the sam ...
- UT源码+105032014070
设计三角形问题的程序 输入三个整数a.b.c,分别作为三角形的三条边,现通过程序判断由三条边构成的三角形的类型为等边三角形.等腰三角形.一般三角形(特殊的还有直角三角形),以及不构成三角形.(等腰直角 ...
- SonarQube使用教程
SonarQube是管理代码质量一个开放平台,可以快速的定位代码中潜在的或者明显的错误,本文将会介绍一下这个工具的安装.配置以及使用. 一.SonarQube的安装使用: 下载地址:http://ww ...
- ①Jenkins集成—入门安装使用
一.什么是Jenkins jenkins是一个广泛用于持续构建的可视化web工具,持续构建说得更直白点,就是各种项目的"自动化"编译.打包.分发部署.jenkins可以很好的支持各 ...
- 一个filebeat实例 设置多topic设置
方法1:一实例多topic: https://discuss.elastic.co/t/filebeat-5-0-output-to-kafka-multiple-topics/67934 The d ...
- 解决hue查询中文报错问题
hue 4.0查询查询中包含中文报一下错误 (1366, Incorrect string value: \\xE4\\xBA\\xAC\\xE4\\xB8\\x9C... for column se ...