每日算法之二十三: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
跟链表的成对转换有异曲同工之意。这里主要考虑的有两点:
1)链表逻辑转换的调用和操作
2)不要存在悬浮指针和断开链接
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
void reverse(ListNode * first,ListNode * end)//把k个元素置逆
{
ListNode * p1 = first;
ListNode * p2;
while(p1 != end&&p1!=NULL)//避免出现悬浮指针,也就是不要对空指针操作
{
p2 = p1->next;
p1->next = end->next;
end->next = p1;
p1 = p2;
}//最后first指针在最后了,指向了兴许链表
}
ListNode *reverseKGroup(ListNode *head, int k) {
if(head == NULL || k<2)
return head;
ListNode * first = head;
ListNode * end = first->next;
ListNode * pre = NULL;
int i = 0;
while(i<k-2&&end!=NULL)//end要指向第k个节点
{
end = end->next;
i++;
}
while(NULL != end)
{
if(first == head)//仅仅有一次
head = end;
else pre->next = end;
reverse(first,end);
pre = first;
first = first->next;
if(first == NULL)
break;
end = first->next;
i =0;
while(i<k-2&&end!=NULL)
{
end = end->next;
i++;
}
}
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 算法思想:基本操作就是链表 ...
- JAVA常见算法题(二十三)
package com.xiaowu.demo; /** * 给一个不多于5位的正整数,要求:①求它是几位数:②逆序打印出各位数字. * * * @author WQ * */ public clas ...
- 每日算法之二十六:Substring with Concatenation of All Words
变相的字符串匹配 给定一个字符串,然后再给定一组同样长度的单词列表,要求在字符串中查找满足下面条件的起始位置: 1)从这个位置開始包括单词列表中全部的单词.且每一个单词仅且必须出现一次. 2)在出现的 ...
- k近邻算法C++二维情况下的实现
k近邻算法C++二维实现 这是一个k近邻算法的二维实现(即K=2的情况). #include <cstdio> #include <cstring> #include < ...
- 【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 ...
- 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. ...
- No.025: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 ...
- [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 ...
随机推荐
- 尝试用Gearman实现分布式处理(PHP)[转]
本文需要你已对Gearman有个大致了解. 顺便再推荐两篇参考文章http://hi.baidu.com/thinkinginlamp/blog/item/ff49972b9e7378f3e6cd40 ...
- bzoj 2286
第一道"虚树"题目(好吧,我也不知道这是不是虚树,但和虚树的思想肯定是一样的,都是简化树结构) 这一类算法核心思想都是简化树结构,只取我们必须的节点和一些信息,然后在简化后的树结构 ...
- [转]Android:异步处理之AsyncTask的应用(二)
2014-11-07 既然UI老人家都这么忙了,我们这些开发者肯定不能不识趣的去添乱阻塞UI线程什么的,否则UI界面万一停止响应了呢——这不是招骂的节奏么?!所以我们知道用Handler+Th ...
- SCOJ 4493: DNA 最长公共子串 后缀自动机
4493: DNA 题目连接: http://acm.scu.edu.cn/soj/problem.action?id=4493 Description Deoxyribonucleic acid ( ...
- 所有浏览器打开后自动打开115.29.163.152/404.html这个网页,然后自动跳转到hao123 解决办法
这几天,电脑所有浏览器打开后自动打开115.29.163.152/404.html这个网页,然后自动跳转到hao123这个网页,网上查询没解决办法.开始自己找,搜索注册表,都无效,最后下载软件Hija ...
- Newtonsoft.Json序列化和反序列
这里下载:http://www.newtonsoft.com/products/json/安装: 1.解压下载文件,得到Newtonsoft.Json.dll 2.在项目中添加引用.. 序列化 ...
- object-c语言的nonatomic,assign,copy,retain的区别
nonatomic: 非原子性访问,不加同步,多线程并发访问会提高性能.如果不加此属性,则默认是两个访问方法都为原子型事务访问. (atomic是Objc使用的一 ...
- 利用Lucene与Nutch构建简单的全文搜索引擎
文章地址 1.简介 本次实现分为两个部分,第一个部分是利用Lucene构建一个全文的搜索引擎,另外一部分则是利用Nutch实现同样的功能.由于Lucene并不是一个可以直接运行的程序,且不具备爬虫和文 ...
- QMsgPack简介
QMsgPack简介 首先,关于MessagePack协议,访问http://msgpack.org可以了解详细的格式约定及各种语言的实现. MessagePack协议号称比JSON快,但速度的快慢这 ...
- OpenSSL再曝CCS注入漏洞-心伤未愈又成筛子
太戏剧了,昨晚看了佳片有约,还不错,2012版的<完美回顾>,像我这样的人依旧选择用电视或者去影院看电影,在没有中间插播广告的时候,体验憋尿得过程中,总是能突然有非常多的想法,这是用电脑或 ...