LeetCode OJ:Reverse Nodes in k-Group(K个K个的分割节点)
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
k个k个的节点反转,即可,用到的记录节点可能比较多,写的比较乱,代码如下所示:
/**
* 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) {
if(k == ) return head;
if(!head || !head->next)
return head;
int len = ;
ListNode * p1 = head;
while(p1){
p1 = p1->next;
len++;
}
//如果k比总长度还要长,那么直接返回即可
if(len < k) return head;
ListNode * prev = NULL;
ListNode * curr = head;
ListNode * tmpNode = NULL;
ListNode * ret = new ListNode(-);
ListNode * helper = curr;
ListNode * helperPre = NULL;
while(len >= k){
for(int i = ; i < k; i++){//区域反转
tmpNode = curr->next;
curr->next = prev;
prev = curr;
curr = tmpNode;
}
if(!ret->next){//第一次手下记下的是首节点的位置
ret->next = prev;
}else{
helperPre->next = prev;//以后将上一部分的尾节点连接到此部分的首节点上面
}
helper->next = curr;
prev = helper;
helper = curr;
helperPre = prev;
len -= k;
}
helperPre->next = curr;//将上次的尾节点域剩下的节点连接起来
return ret->next;
}
};
LeetCode OJ:Reverse Nodes in k-Group(K个K个的分割节点)的更多相关文章
- 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 每k个一组翻转链表
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k ...
- 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 ...
- [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 k个一组翻转链表
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k ...
- [LeetCode 题解]: Reverse Nodes in K-Groups
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a li ...
- leetcode 【 Reverse Nodes in k-Group 】 python 实现
原题: 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
1 题目: Given a linked list, reverse the nodes of a linked list k at a time and return its modified li ...
- 【leetcode】Reverse Nodes in k-Group
Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked list k at a time and ret ...
- 【leetcode】Reverse Nodes in k-Group (hard)☆
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...
随机推荐
- cocos2d-x 2.x.x 新建工程 android下的 org文件夹丢失
cocos2d-x 2.x.x 新建工程之后... 打开android项目..会发现src下没有org文件... 解决方法一: cocos2d-x-2.2.0\cocos2dx\platform\an ...
- webpack打包静态资源和动态资源
1.对于静态引用的资源: <img src = "static/modelname/imgname.png"> // 修改为下面的写法 <img src = &q ...
- jsp 小记
1. select 默认选中: <select name="skills" multiple="true"> <option value=&q ...
- ES6 利用 Set 数组去重法
例子: const set = new Set(); [2, 3, 5, 4, 5, 2, 2].forEach(x => set.add(x) ); const arr = [...set]; ...
- netty11---管道
客户端: package com.server; import java.net.Socket; public class Client { public static void main(Strin ...
- Job流程:决定map个数的因素
此文紧接Job流程:提交MR-Job过程.上一篇分析可以看出,MR-Job提交过程的核心代码在于 JobSubmitter 类的 submitJobInternal()方法.本文就由此方法的这一句代码 ...
- django中的分页设置
1.在控制台中的展示 from django.core.paginator import Paginator iter = 'abcdefghijklmn' inator = Paginator(it ...
- [小问题笔记(二)] 可能导致DropDownList的SelectedIndexChanged事件不触发的几种情况
遇到SelectedIndexChanged事件没有触发,可以依次检查以下几种情况是否在程序中出现. 一.DropDownList的不同option设置了相同的value . 二.没有写 AutoPo ...
- 秒懂算法3——插入排序(C#实现)
算法思路: 将n个元素分成[已排序]和[未排序]两部分.每次将[未排序]中的一个元素取出,插入到已排序中的相应位置.直至所有元素排序完毕. [已排序] [未排序] { { a[0] } ...
- SQL优化- in和not in
in不会导致索引失效,但最终数据库会将in语句解析为or语句,eg: select * from T_MAIN_PROCESS t where t.audit_status_code in ('05' ...