【Leetcode】【Hard】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 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的更多相关文章
- [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题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【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 ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- 【leetcode刷题笔记】Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题 ...
- 【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 ...
- 【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 ...
- 【LeetCode每天一题】Reverse Integer(反转数字)
Given a 32-bit signed integer, reverse digits of an integer. Example 1: ...
随机推荐
- 自学springboot
参考资料 https://www.renren.io/guide/
- CSAPP阅读笔记-栈帧-来自第三章3.7的笔记-P164-P176
1.基本结构: 如上图所示,是通用的栈帧结构.大致分两块,调用者函数P和被调用者函数Q. 对P来说,要做的工作是把传递参数中多于6个的部分压栈,随后把Q返回时要执行的下一条指令的地址压栈. 对Q来说, ...
- 2019.03.29 读书笔记 关于override与new
差异:override:覆盖父类分方法,new 隐藏父类方法. 共同:都不能改变父类自身方法. public class Test { public string Name { get; set; } ...
- Oracle运算符收录(易忘记,但是又很重要的运算符)
Create Table Test6( id ), name ), age ), sex ) ) 1. || 符 字符串连接字符串,注意:文字和日期一定嵌入在单引号里面 select ID,Nam ...
- Robot Framework(AutoItLibrary安装)
RobotFramework下安装AutoItLibrary库 1.安装pythonwin32 在下载地址:http://sourceforge.net/projects/pywin32/files/ ...
- 导入数据到HBase的方式选择
Choosing the Right Import Method If the data is already in an HBase table: To move the data from one ...
- bootstrap table 以及xEdittable的应用
以前一直没有用过 bootstrap 表格框架,因为项目css框架用的是bootstrap,为考虑到统一性的原因,所以选用了这个框架 步骤: 第一步:引用 <link href=" ...
- grep常用命令总结与基础正则
grep 'str'用来选取含有str的行 参数: -v 反向选取 -n 显示行号 -c 计算查找到字符串的次数 -i 选取时不区分大小写 基础正则表达式符: [list] 从字符集合里选出任意一个字 ...
- OOP_由C到C++
由C到C++ OOP第一课 C语言的局限 C++的特点 C++的程序特征 C++程序的结构特性 C++程序的编辑.编译和运行 ⭐C++对C的补充 C语言的局限 类型检查机制相对较弱,使得程序中的一些错 ...
- 压缩图片或pdf
压缩图片或pdf { /// <summary> /// 压缩图片或pdf大小的Level /// </summary> public enum ReduceSizeLevel ...