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. 2019年UX设计新趋势

    UX设计总是在不断变化中.最近短短两年的时间里,我们已经看到,很多地方都大规模采用颠覆性技术,比如语音用户界面,混合现实和智能家居设备.设计这些体验的实际过程可能保持不变,但新技术的出现引发了新的行为 ...

  2. RMQ(求区间最值问题)

    学习博客:https://blog.csdn.net/qq_31759205/article/details/75008659 RMQ(Range Minimum/Maximum Query),即区间 ...

  3. jQuery插件的开发(一)

    jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法.jQuery的全局函数就是属于jQuery命名空间的函数,另一种是对象级 ...

  4. oracle系统包——dbms_transaction用法

    用于在过程,函数和包中执行sql事务处理语句. 1.read_only用于开始只读事务,其作用与sql语句set transaction read only完全相同2.read_write用于开始读写 ...

  5. JavaScript弹出层

    1.这个弹出层就是一个DIV 2.看需要什么效果 2.1.如果是仅仅需要弹出层,而背后的网页同样可以点击,那么只需要一个DIV即可,效果如图: 2.2.需要一层透明背景,而后面的网页只能看不能点,效果 ...

  6. MYSQL常用函数以及分组操作

    SELECT CONVERT(",SIGNED); SELECT CAST(" AS SIGNED); SELECT ; SELECT LENGTH("姜浩真帅!&quo ...

  7. http所有请求头在Console中打印

    1.目标:将http中的请求头全部打印在Console中 2.基本语句 //1.获得指定的头 String header = response.getHeader("User-Agert&q ...

  8. Servlet开发(一)

    1. Servlet简介 Servlet是服务器端程序,主要用来交互式地浏览和修改数据,生成动态web内容.Servlet是SUN公司提供的一个接口,广义的Servlet可以指任何实现了Servlet ...

  9. vc++返回模块路径

    #include "stdafx.h"#include <Windows.h>#include <string.h>const int MAXPATHLEN ...

  10. 运行tomcat7w.exe,提示:指定的服务未安装unable to open the service tomcat7

    这是服务没安装,到tomcat的bin目录下运行 service.bat install 即可