每日算法之二十三: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 ...
随机推荐
- Codeforces Round #346 (Div. 2) G. Fence Divercity dp
G. Fence Divercity 题目连接: http://www.codeforces.com/contest/659/problem/G Description Long ago, Vasil ...
- JAVA容器-模拟LinkedList实现(双链表)
概述 LinkedList实质上就是双向链表的拓展的实现,我们将关注一下问题.LinkedList 1.双向链表怎么来实现插入.删除.查询? 2.利用二分法提高查询效率. 3.不同步,线程不安全,需要 ...
- MyEclipse2015创建配置Web+Maven项目
首先我的MyEclipse版本是2015 stable 2.0,在MyEclipse中创建Maven项目通常有两种常见的方式,它们分别是: New Maven Project New Web Pro ...
- iScroll4插件的使用实例
iScroll是Matteo Spinelli开发的一个滚动插件,使用原生js编写,其不依赖与任何js框架.iScroll 4 完全重写了iScroll这个框架的原始代码.旨在解决移动webkit系浏 ...
- IOS常用的第三方开发库
开发几个常用的开源类库及下载地址: 1.json json编码解码2.GTMBase64 base64编码解码3.TouchXML xml解析4.SFHFKeychainUtils 安全保存用户密码到 ...
- ZigBee和Z-Wave的区别与未来
http://tech.c114.net/164/a702667.html ZigBee和Z-Wave短距离无线技术都用于远程监控和控制,但两种技术的规格和应用却不同.在美国应用越来越广泛的家庭局域网 ...
- TPS61040/61041 开关电源稳压器(DC-DC) ADJUST
Variable Control Voltage Output Voltage Adjust This method is accomplished by connecting a variable ...
- struts2自动接收表单数据
提交页面主要代码: <form method="post" action="messageAction"> <input type=&qu ...
- .Net4.0并行库介绍——Cancellation Framework
在.net 4.0中,引入了一个新的类CancellationToken,这个类基本上集成了我们各种常用的取消方式,在并发任务中非常有用. 同步模式下的取消: 一种比较常见的需要支持取消功能的的是一些 ...
- java Integer包装类装箱的一个细节
原文:https://www.cnblogs.com/JackPn/p/9392145.html java有八个基本数据类型,每个都有对应的一个包装类,比如int对应的Integer.从jdk1.5开 ...