【一天一道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 ...
随机推荐
- ZooKeeper之(六)应用实例
6.1 Java API 客户端要连接 Zookeeper服务器可以通过创建 org.apache.zookeeper.ZooKeeper 的一个实例对象,然后调用这个类提供的接口来和服务器交互. Z ...
- 有一个排序二叉树,数据类型是int型,如何找出中间大的元素。
void tree2Dll(TNode* root, TNode*& tail) { if (!root) { return; } if (root->left) { tree2Dll( ...
- 解决HTML外部引用CSS文件不生效问题
作为一个前端小白,鼓捣了几天前端..今天突然发现我深信不疑的东西,竟然出现了问题..就比如我在css目录下面写了一个css样式文档:style.css.这时里面只有一句话: body { backgr ...
- Spinner控件详解
Spinner控件详解 效果图 修改Spinner样式 在介绍之前,先看一下系统原生的样式 6.x & 5.x系统样式 4.x系统样式 官方文档 XML属性 方法 描述 android:dro ...
- Android新特性Instant Run详解
关于 Instant Run Android Studio 2.0 中引入的 Instant Run 是 Run 和 Debug 命令的行为,可以大幅缩短应用更新的时间.尽管首次构建可能需要花费较长的 ...
- list标准函数的模拟
;反序 ( ) -> ( ) (define (rvs x) (let recur ((x x)(res '())) (if (null? x) res (recur (cdr x) (cons ...
- Android开发学习之路--Drawable mutations
时间过得很快,明天终于可以拿到房子了,交完这次房租,也可以成为房东了,看看博客也好久没有更新了,最近一直在整机器人,也没有太多时间整理博客. 今天下午和同事一起遇到了一个问题,就是明明没有改变 ...
- C控制台实现模拟平抛运动算法
平抛运动这个相信读了高中物理都知道这个概念了,详细的我就不说了,不明白的看看百度: 平抛运动 接下来看看用控制台实现的平抛运动算法: #include <stdio.h> #include ...
- 关于AndroidSDK配置时的tools目录下找不到adb.exe的错误
欢迎关注公众号,每天推送Android技术文章,二维码如下:(可扫描) 在配置android SDK的时候,有时会发现在android-sdk-windows\tools目录下并没有adb.exe,这 ...
- python 多进程 logging:ConcurrentLogHandler
python 多进程 logging:ConcurrentLogHandler python的logging模块RotatingFileHandler仅仅是线程安全的,如果多进程多线程使用,推荐 Co ...