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个结点进行链表反转(链表)的更多相关文章

  1. [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  ...

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

  3. [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  ...

  4. [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  ...

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

  6. [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 ...

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

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

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

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

随机推荐

  1. leetcode144

    /** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNo ...

  2. struts2结合axis开发webservice

    第一步:引入axis的依赖jar包  第二步:修改web.xml文件  <listener> <listener-class>org.apache.axis.transport ...

  3. Python小代码_15_遍历指定路径下的所有文件和文件夹,并格式化输出文件路径文件名和文件夹名,文件大小,修改时间

    遍历指定路径下的所有文件和文件夹,并格式化输出文件路径文件名和文件夹名,文件大小,修改时间 import osimport datetime def print_tree(dir_path): for ...

  4. Oracle 中 over() 函数

    :first-child { margin-top: 0; } blockquote > :last-child { margin-bottom: 0; } img { border: 0; m ...

  5. Mips下交叉编译dropbear

    1. 编译zlib-1.2.8 在编译dropbear的时候,会遇到“configure: error: *** zlib missing - install first or check confi ...

  6. algorithm notes

    1.算法可视化 https://visualgo.net/en

  7. Intent对象若干数据项的含义总结

    Intent作为组件之间通信的手段和协议,包含了诸如Action.Data.Type.Category.Component.Extras和Flags等数据项,各自拥有各自的含义和作用.当调用组件发出一 ...

  8. Django框架 之 logging配置

    Django框架 之 logging配置 logging配置文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 2 ...

  9. 第一次C语言作业:博客随笔

    1)你觉得大学和高中有什么差别?具体学习上哪? 大学自主学习较多,锻炼自己独立的品质.在学习上,增加了课程的深度和难度,由更多的活动. 2)我希望大学的师生关系是?阅读上述博客后对师生关系有何感想? ...

  10. Media Queries 媒体类型

    引用方法:1.<link rel="stylesheet" type="text/css" href="style.css" medi ...