Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表)
Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表)
题目描述
已知一个链表,每次对k个节点进行反转,最后返回反转后的链表
测试样例
Input: k = 2, 1->2->3->4->5
Output: 2->1->4->3->5
Input: k = 3, 1->2->3->4->5
Output: 3->2->1->4->5
详细分析
按照题目要求做就行了:比如k=2,首先[1->2->3->4->5]分组为[1->2],[3->4],[5],然后每组反转[2->1],[4->3],[5],最后输出反转后的链表[2->1->4->3->5]。
算法实现
class Solution {
public:
ListNode* reverseKGroup(ListNode* head, int k) {
if(head==nullptr || k==1){
return head;
}
ListNode * cur = head;
while(true){
partialReverse(cur,k);
for(int i=0;i<k;i++){
cur=cur->next;
if(cur==nullptr){
return head;
}
}
}
}
// it's a closed interval
void partialReverse(ListNode * start,int k){
// 1->2
// 1->2->3->4->5
std::stack<int> vec;
ListNode * temp = start;
for(int i=0;i<k;i++){
vec.push(temp->val);
temp = temp->next;
if(i!=k-1 && temp==nullptr){
return;
}
}
for(int i=0;i<k;i++){
int val = vec.top();
vec.pop();
start->val = val;
start=start->next;
}
}
};
Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表)的更多相关文章
- [LeetCode] 25. 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. 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 ☆☆☆
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每k个节点反转一下
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 Add to List (划分list为k组)
题目链接: https://leetcode.com/problems/reverse-nodes-in-k-group/?tab=Description Problem :将一个有序list划分 ...
- [LeetCode]25. 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. k ...
- [leetcode 25]Reverse Nodes in k-Group
1 题目: Given a linked list, reverse the nodes of a linked list k at a time and return its modified li ...
- Java [leetcode 25]Reverse Nodes in k-Group
题目描述: Given a linked list, reverse the nodes of a linked list k at a time and return its modified li ...
- [Leetcode][Python]25: Reverse Nodes in k-Group
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 25: Reverse Nodes in k-Grouphttps://oj. ...
随机推荐
- 「小程序JAVA实战」 小程序wxss样式文件的使用(七)
转自:https://idig8.com/2018/08/09/xiaochengxu-chuji-07/ 细说下微信小程序的wxss样式文件.源码:https://github.com/liming ...
- BurpSuite系列(十一)----Project options模块(项目选择)
一.简介 Project options主要用来对Project的一些设置. 二.模块说明 Project options主要由五个模块组成: 1.Connections 连接2.HTTP3.SSL4 ...
- Java “hello word” 第一天
//新建包和类 //java是包,c#是命名空间package test1;/** * 需求:练习一个hello word * 思路: * 1.定义一个类,因为java程序都是以类的形式存在的,类的形 ...
- 1-在eclipse里面配置python(最详细)
最近有时间打算学下python,打算学当然是得先搞好开发工具,网上搜一波,发现许多ide,居然可以在eclipse下写python,由于最近一直在搞java,所以已经装了eclipse,所以打算就在e ...
- 面试题:Java多线程必须掌握的十个问题 背1
一.进程与线程?并行与并发? 进程代表一个运行中的程序,是资源分配与调度的基本单位.进程有三大特性: 1.独立性:独立的资源,私有的地址空间,进程间互不影响. 2.动态性:进程具有生命周期. 3.并发 ...
- jQuery基础教程-第8章-003Providing flexible method parameters
一.The options object 1.增加阴影效果 (function($) { $.fn.shadow = function() { return this.each(function() ...
- Classification and Prediction
# coding: utf-8 # In[128]: get_ipython().magic(u'matplotlib inline') import pandas as pd from pandas ...
- SEO网站结构优化
结构布局优化:用扁平化结构(层次结构超过三层小蜘蛛就不愿意爬了) 控制首页链接数量(中小网站100以内,页面导航.底部导航.锚文字链接等) 扁平化的目录层次(小蜘蛛跳转3次可以到达网站内任何一个内页, ...
- System.Web.UI.Page事件执行顺序
#region OnPreInit 第一步(显式重写,文章下面有隐式重写) protected override void OnPreInit(EventArgs e) { //检查 IsPostBa ...
- (转)那天有个小孩教我WCF[一][1/3]
原文地址:http://www.cnblogs.com/AaronYang/p/2950931.html 既然是小孩系列,当然要有一点基础才能快速掌握,归纳,总结的一个系列,哈哈 前言: 第一篇嘛,不 ...