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,功能是反转子链表前K个结点,剩余结点不变;如果不够K个结点,则保持原链表。

之后循环调用此函数。

代码:

 /**
* 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) {
if (head == NULL || head->next == NULL || k == )
return head;
ListNode* newhead = new ListNode();
ListNode* newlist = newhead;
ListNode* curNode = head;
while (curNode != NULL) {
newlist->next = reverseListKNodes(curNode, k);
// After "reverseListKNodes" func, "curNode" will be the end of reversed sublist
newlist = curNode;
curNode = curNode->next;
} head = newhead->next;
delete newhead;
return head;
} ListNode* reverseListKNodes(ListNode* sub_head, int k) {
if (sub_head == NULL || sub_head->next == NULL)
return sub_head; ListNode* newhead = NULL;
ListNode* nodesleft = sub_head;
ListNode* curNode = NULL; for (int i = ; i < k; ++i) {
if (nodesleft == NULL)
return reverseListKNodes(newhead, i);
curNode = nodesleft;
nodesleft = nodesleft->next;
curNode->next = newhead;
newhead = curNode;
} sub_head->next = nodesleft;
return newhead;
}
};

【Leetcode】【Hard】Reverse Nodes in k-Group的更多相关文章

  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题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  4. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  5. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  6. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  7. 【leetcode刷题笔记】Merge k Sorted Lists

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题 ...

  8. 【leetcode刷题笔记】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. If ...

  9. 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number

    [Q7]  把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...

  10. 【LeetCode每天一题】Reverse Integer(反转数字)

    Given a 32-bit signed integer, reverse digits of an integer. Example 1:                              ...

随机推荐

  1. 微信小程序踩坑

    微信小程序自定义属性data-xx使用注意事项 data-xx采用驼峰式命名时,数据传递打印显示(以jxsName与jxsname打印显示对比) data-xx全小写命名时,数据传递打印显示

  2. 关于echart x轴溢出的解决办法

    现象 溢出挤在一起,或者默认多余的不显示,如果需要强制显示,需要设置 axisLabel: { interval: 0, } 解决办法: 一.旋转一定的角度 axisLabel: { interval ...

  3. SSL评测

    首先在这个网站上测试一下自己的服务器究竟处于什么水平 https://www.ssllabs.com/ssltest/

  4. 《c++primer》疑惑记录

    第4章 96页,数组维数为变量 第8章 246. IO对象不可复制.赋值原因是类设计时复制构造函数.赋值函数是私有的,为什么这么设计呢? 251. tie举例 第15章 484 派生类可以恢复,但不可 ...

  5. Visual Studio2017中如何让ADO.NET实体数据模型[EntityFramework]支持MariaDB&MySQL数据源

    近期由于工作需要,需要重新修改设计系统的ADO.NET实体数据模型.edmx文件中间,在完成实际中途遇到一些实际使用问题,特此记录. 1. Visual Studio 2017 无法以实体设计模式打开 ...

  6. C#根据用户输入字符串,输出大写字母有几个,小写字母有几个

    static void Main(string[] args) { // 根据用户输入字符串,输出大写字母有几个,小写字母有几个. Console.WriteLine("请输入一行英文代码& ...

  7. 一个骚气的前端JS代码生成网站

    生成Javascript 颜文字代码 稍微试了试 原本的代码: alert("Hello, JavaScript") 转换后代码 ゚ω゚ノ= /`m´)ノ ~┻━┻ //*´∇`* ...

  8. ASP.NET MVC4 新手入门教程之八 ---8.向模式中添加验证

    在这本部分会将验证逻辑添加到Movie模式,和你会确保验证规则执行任何时候用户试图创建或编辑使用该应用程序的一部电影. 保持事物的干练性 ASP.NET MVC 的核心设计信条之一是 DRY(”Don ...

  9. Charles破解(转)

    NB的Charles是一款付费软件.但…本文将讲解如何破解Charles.注:虽然与文章内容相悖,但还是希望大家能购买正版软件,毕竟都是做软件开发的,何必自断生路,要有版权意识. 环境信息: Mac ...

  10. MongoDb 学习笔记(一) --- MongoDb 数据库介绍、安装、使用

    1.数据库和文件的主要区别 . 数据库有数据库表.行和列的概念,让我们存储操作数据更方便 . 数据库提供了非常方便的接口,可以让 nodejs.php java .net 很方便的实现增加修改删除功能 ...