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
又是一道链表的题,关于链表的题好像都是关于指针的,题目都不难吧,主要是需要细心,因为不难,所以代码没有自己写,这里还可以考虑之前做过的链表翻转m至n那道题,然后调用函数就解决了······················
下面的代码的思路是用子函数挨个处理每个K段。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverse(ListNode* head){
ListNode* prev = nullptr;
ListNode* nowNode = head;
while(nowNode){
ListNode* next = nowNode -> next;
nowNode -> next = prev;
prev = nowNode;
nowNode = next;
}
return prev;
}
ListNode *reverseKGroup(ListNode *head, int k) {
if(head == nullptr || head -> next == nullptr || k < ) return head; int cnt = ;
ListNode* nowHead = head;
ListNode* nowNode = head;
while(nowNode && cnt < k){
cnt ++;
nowNode = nowNode -> next;
}
if(nowNode && cnt == k){
ListNode* tail = reverseKGroup(nowNode -> next , k);
nowNode -> next = nullptr;
head = reverse(head);
nowHead -> next = tail;
}
return head;
}
};
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 算法思想:基本操作就是链表 ...
- 【Reverse Nodes in k-Group】cpp
题目: Given a linked list, reverse the nodes of a linked list k at a time and return its modified list ...
- [LintCode] 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 ...
- Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表)
Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表) 题目描述 已知一个链表,每次对k个节点进行反转,最后返回反转后的链表 测试样例 Inpu ...
- 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 笔记系列六 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. ...
- LeetCode: Reverse Nodes in k-Group 解题报告
Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked list k at a time and ret ...
- 25.Reverse Nodes in k-Group (List)
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...
- Leetcode 25/24 - Reverse Nodes in k-Group
题目描述 Leetcode 24 题主要考察的链表的反转,而 25 题是 24 的拓展版,加上对递归的考察. 对题目做一下概述: 提供一个链表,给定一个正整数 k, 每 k 个节点一组进行翻转,最后返 ...
随机推荐
- Hive(六)hive执行过程实例分析与hive优化策略
一.Hive 执行过程实例分析 1.join 对于 join 操作:SELECT pv.pageid, u.age FROM page_view pv JOIN user u ON (pv.useri ...
- 【bzoj4894】天赋
Portal-->bzoj4894 Solution 这题的话其实,一句话题意就是求..外向树(方向是根往叶子).. 然后关于有向图的生成树计数的话,求外向树就是将度数矩阵改成入度,内向树就是改 ...
- Meeting HDU - 5521 虚点建图
Problem Description Bessie and her friend Elsie decide to have a meeting. However, after Farmer John ...
- std::sort运行出core(segment fault)
http://note.youdao.com/noteshare?id=6aae09345e85ab55fe24ac959118a747
- 出现错误日志:The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path
tomcat6出现错误日志: 信息: The APR based Apache Tomcat Native library which allows optimal performance in ...
- 构造+分块思想 Codeforces Round #319 (Div. 1) C
http://codeforces.com/contest/576/problem/C 题目大意: 给你一个曼哈顿距离的图,然后要求你找到一个链,链穿了所有的点 然后要求这链的长度<=25*10 ...
- LightOJ 1017 - Brush (III) 记忆化搜索+细节
http://www.lightoj.com/volume_showproblem.php?problem=1017 题意:给出刷子的宽和最多横扫次数,问被扫除最多的点是多少个. 思路:状态设计DP[ ...
- PHP提取链接批量下载
2014年年初的时候,曾经受委托完成一个视频网站,那时最大的技术障碍是一个大视频比如500MB,在一个带宽环境不怎么快的服务器(比如1Mbps)上提供播放的问题. 这里会遇到两种情况,第一种情况是客户 ...
- Plan
相册Gallery 标签页 Slide 播放器 Dialog 放大镜 http://sc.chinaz.com/info/130123265570.htm
- bootstrap-select,selectpicker 用法详细:通过官方文档翻译
用过selectpicker的都说好~但是网上中文的教程又找不到比较完整的用法,于是去官网看了下 顺便弄过来翻译一下: 选项可以通过数据属性或JavaScript传递.对于数据属性,附加选项名称dat ...