LeetCode Reverse Nodes in k-Group 每k个节点为一组,反置链表
题意:给一个单链表,每k个节点就将这k个节点反置,若节点数不是k的倍数,则后面不够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(k==) return head;
ListNode* p=head;
int cnt=;
while(cnt!=k && p) //找到下一段的链表头
{
cnt++;
p=p->next;
}
if(cnt==k) //进行反置
{
ListNode* nextp=head->next, frontp=head, qq;
while(nextp!=p) //p是下一段链表的开头
{
qq=nextp->next;
nextp->next=frontp;
frontp=nextp;
nextp=qq;
}
head->next=reverseKGroup( p ,k );
return frontp; //返回新链表的头指针
}
else return head; //不够k个
}
};
AC代码
LeetCode Reverse Nodes in k-Group 每k个节点为一组,反置链表的更多相关文章
- [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 ...
- [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 ...
- 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 ...
- 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. k ...
- 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 ...
- 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 Reverse Nodes in k-Group python
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...
- LeetCode Reverse Linked List II 反置链表2
题意:将指定的一段位置[m,n]的链表反置,返回链表头. 思路:主要麻烦在链表头,如果要从链表头就开始,比较特殊. 目前用DFS实现,先找到m-1的位置,再找到n+1的位置,中间这段就是否要反置的,交 ...
- LeetCode Reverse Linked List (反置链表)
题意: 将单恋表反转. 思路: 两种方法:迭代和递归. 递归 /** * Definition for singly-linked list. * struct ListNode { * int va ...
随机推荐
- 用ScriptEngine在java中和javascript交互的例子(JDK6新特性)
package demo7; import java.util.Arrays; import java.util.List; import javax.script.Invocable; import ...
- CSS Ruler 前端工具
CSS Ruler是一款在线的CSS单位工具. CSS Ruler 彩蛋爆料直击现场 http://katydecorah.com/css-ruler/
- POJ 1458
#include <iostream> #include <string> #define MAXN 1000 using namespace std; string s_1; ...
- 如何理解 MySQL 中的 <=> 操作符?
问题 : 我在看以前的一个开发者的代码时看到 WHERE p.name <=> NULL 在这个查询语句中 <=>符号是什么意思啊?是不是和 =号是一样啊?还是一个语法错误啊? ...
- 8 Types Of Friends You Need To Have in Your Life
8 Types Of Friends You Need To Have in Your Life一生中应该有的8种类型的朋友Did you know that people without frien ...
- 让ie6也支持max-width,和max-height实现图片等比例缩放
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Fatal error: cannot allocate memory for the buffer pool
mysql有时候会被系统kill掉,原因是内存不够了,一般都是Ubuntu出现的,因为Ubuntu吃内存,你们又给的不多.. 咋解决呢? 重启服务器是可以的,起码暂时可以了, 可以考虑加内存,或者增加 ...
- SpringMVC + MyBatis 环境搭建(转)
本文转自:http://blog.csdn.net/zoutongyuan/article/details/41379851 源码地址:https://github.com/starzou/quick ...
- power designer 连接数据库提示“connection test failed”
利用powerdesigner反向生成表结构时,需要mysql连接,配置好连接,测试时直接报:connection test failed”! OS:WIN7 旗舰版 64位 JDK: 64位 Pow ...
- jquery的extend和fn.extend的使用说明
jQuery.fn.extend(object); 对jQuery.prototype进得扩展,就是为jQuery类添加“成员函数”.jQuery类的实例可以使用这个“成员函数”. jQuery为开发 ...