【一天一道LeetCode】#25. Reverse Nodes in k-Group
一天一道LeetCode系列
(一)题目
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->5For k = 2, you should return: 2->1->4->3->5
For k = 3, you should return: 3->2->1->4->5
(二)解题
/**
* 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) {
//主要思想:以K为间距,分别对间距内的链表进行反转
//需要注意的问题:为了防止链表断裂需要记录每一段的首尾元素
ListNode* pret = head;
ListNode* phead = head;//当前待反转的head节点
ListNode* ptail = NULL;//当前待反转的尾节点
ListNode* pnexthead = NULL;.//记录下一段的head
ListNode* plasttail = NULL;//记录前一段的尾节点
if(head == NULL) return NULL;
while(phead!=NULL)
{
int gap=k;
ListNode* p = phead;
while(gap != 1&& p!=NULL) {p = p->next;gap--;}//找到尾节点
if(p!=NULL){
ptail = p;
ListNode* tmp;
pnexthead = p->next;
if(phead == head) pret = reverseK(phead,ptail,pnexthead);//0-k段的时候要记录整个链表的头节点
else
{
tmp = reverseK(phead,ptail,pnexthead);
plasttail->next = tmp;
}
phead = pnexthead;//跳转到下一段
plasttail = ptail;//记录当前段的尾节点
}
else
{
if(plasttail!= NULL) plasttail->next = phead;//将[0-k],[k+1-2k]......链接起来,防止链表断裂
phead = NULL;//如果链接最后凑不够K个元素,则将phead置为NULL
}
}
return pret;
}
ListNode* reverseK(ListNode* phead ,ListNode* &ptail/*注意此处用引用来获得尾节点*/,ListNode* pnexthead)
{
ListNode* prevHead = phead;
ListNode* p = phead->next;
ListNode* pre = phead;
while(1)
{
if(p==pnexthead) {
ptail = pre;//返回尾节点
return prevHead;
}
pre->next = p->next;
p->next = prevHead;
prevHead = p;
p=pre->next;
}
}
};
【一天一道LeetCode】#25. Reverse Nodes in k-Group的更多相关文章
- 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 [Difficulty: Hard]
题目 Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. ...
- 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 每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个一组翻转链表
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k ...
- [leetcode 25]Reverse Nodes in k-Group
1 题目: Given a linked list, reverse the nodes of a linked list k at a time and return its modified li ...
- Java [leetcode 25]Reverse Nodes in k-Group
题目描述: Given a linked list, reverse the nodes of a linked list k at a time and return its modified li ...
- [leetcode]25. 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] 25. 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. k ...
- [LeetCode]25. 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 ...
随机推荐
- Dynamics CRM2016 Web API之获取查找字段的text及选项集的text
本篇再来介绍个web api的功能,关于lookup的text这里只是略带,因为有expand,现有的web api就能实现,主要提的是选项集的text,我们通过基本的查询api查出来的字段值只带有v ...
- Cassandra Secondary Index 介绍
摘要 本文主要介绍cassandra中的索引,物化视图,有些知识点需要对cassandra有基本的认识才能理解.比如数据在cassandra节点中如何分布.如果有不明白的地方可以看本专栏之前文章.或者 ...
- Unable to ignore resources
摘要:分享牛,分享牛系列, Unable to ignore resources Attempted to beginRule: 异常信息处理. 出现Unable to ignore resource ...
- SQL Server 索引维护(1)——如何获取索引使用情况
前言: 在前面一文中,已经提到了三类常见的索引问题,那么问题来了,当系统出现这些问题时,该如何应对? 简单而言,需要分析现有系统的行为,然后针对性地对索引进行处理: 对于索引不足的情况:检查缺少索引的 ...
- c语言求最大公约数
求差判定法. 如果两个数相差不大,可以用大数减去小数,所得的差与小数的最大公约数就是原来两个数的最大公约数.例如:求78和60的最大公约数.78-60=18,18和60的最大公约数是6,所以78和60 ...
- java操作XML文件--读取内容
先把问题贴出来:编写一个可以解析xml及修改xml内容的工具类 由于我以前做过Android应用程序开发,之前也解析过xml文件,所以,这道题不是很难,这篇文章我先解决第一个问 ...
- Cocos2D添加精灵纹理滤镜实现图像复古效果的转换
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 大家知道Cocos2d本身是一个非常强悍的2d游戏引擎,其中自 ...
- Android的ToggleButton和Switch以及AnalogColok和DigitalColok的用法-android学习之旅(二十)
ToggleButton 和Switch简介 ToggleButton 和Switch都是继承了Button,所以他们的属性设置和Button差不多. 分别支持的属性 ToggleButton 的属性 ...
- Android开发小问题集
由于安卓系统比较复杂,开发中会发中会碰见各种小问题,在此做一些记录,只要觉得有必要就会添加进来. 1.触屏鼠标模式和触屏模式 开发android4.3高通400平台时,用atmel_max 640T作 ...
- JDK 7中的文件操作的新特性
文件系统综述 一个文件系统在某种媒介(通常是一个或多个硬盘)上存储和组织文件.如今的大多数文件系统都是以树状结构来存储文件.在树的顶端是一个或多个根节点,在根节点一下,是文件和目录(在Windows系 ...