【一天一道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 ...
随机推荐
- Matlab 2015b 启动时崩溃 MATLAB crashes during startup on Ubuntu 16.04
Matlab 启动时崩溃 MATLAB crashes during startup on Ubuntu Matlab 2015B Ubuntu 16.04 之前解决过,更新后问题又来了. 出 ...
- ejabberd mod_echo 解析
ejabberd mod_echo 解析(金庆的专栏 2016.8)按开发入门的说明,mod_echo是最简单的模块之一.https://docs.ejabberd.im/developer/当然 m ...
- Thread 方法
Thread类的一些被Thread对象调用的方法: 1 public void start() 使该线程开始执行:Java 虚拟机调用该线程的 run 方法. 2 public void run() ...
- Dynamics CRM2016 Web API之Retrieve Multiple
之前的博文只介绍了通过记录的primary key来查询单条记录或者单个属性值,本篇介绍多条记录的查询方法 var filter = "?$filter=name eq '123'" ...
- ubuntu安装qq
安装的版本是国际版 1.安装依赖库 sudo apt-get install libgtk2.0-0:i386 sudo apt-get install lib32ncurses5 2.下载 下载链接 ...
- springmvc文件上传和拦截器
文件上传 用到这两个包 配置视图解析器:springmvc配置文件配置 <!-- id必须要是"multipartResolver" --> <bean id=& ...
- RocketMQ,JStorm与Tair使用笔记
关于RocketMQ 启动mq nohup sh mqnamesrv -n 10.150.0.94:9876 & nohup sh mqbroker -n 10.150.0.94:9876 ...
- DBoW2应用
图像对应的bag-of-words向量\(v_t\) 假设词典总共有\(W\)个单词,那么每一幅图像能够用一个\(W\)维的向量表示 \((t_1, t_2, t_3, ..., t_W)\)其中 \ ...
- 15 Action View 以及监听 的使用
menu 代码 <menu xmlns:android="http://schemas.android.com/apk/res/android" > <!-- a ...
- 【IOS 开发】基本 UI 控件详解 (UISegmentedControl | UIImageView | UIProgressView | UISlider | UIAlertView )
转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/50163725 一. 分段控件 (UISegmentedControl) 控件展 ...