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.
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
==================
思路:
没有什么坑,就是编码麻烦,
代码:
void help_reverseKGroup(ListNode *prev,ListNode *curr,int k){
ListNode *p = prev->next;
prev->next = curr->next;
for(int i = ;i<k;i++){
ListNode *tmp = p->next;
p->next = prev->next;
prev->next = p;
p = tmp;
}
}
ListNode* reverseKGroup(ListNode* head, int k) {
if(k<=) return head;
if(head==nullptr || head->next==nullptr)return head;
ListNode dummy(-);
dummy.next = head;
ListNode *curr,*prev;
curr = head;
prev = &dummy;
while(curr){
int i = ;
for(;i<k;i++){
if(curr->next) curr = curr->next;
else break;
}
if(i!=k) break;
//showList(dummy.next);cout<<"k1"<<endl;
help_reverseKGroup(prev,curr,k);
//showList(dummy.next);cout<<"k2"<<endl;
for(i = ;i<k;i++){
prev = curr;
curr = curr->next;
}
//showList(head);cout<<"k3"<<endl;
}///while
return dummy.next;
}
25. 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 算法思想:基本操作就是链表 ...
- Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表)
Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表) 题目描述 已知一个链表,每次对k个节点进行反转,最后返回反转后的链表 测试样例 Inpu ...
- [Leetcode][Python]25: Reverse Nodes in k-Group
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 25: Reverse Nodes in k-Grouphttps://oj. ...
- 24. Swap Nodes in Pairs(M);25. Reverse Nodes in k-Group(H)
24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
- 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划分 ...
- 25.Reverse Nodes in k-Group (List)
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...
- 25. Reverse Nodes in k-Group (JAVA)
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 [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 (2 solutions)
Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked list k at a time and ret ...
随机推荐
- php中读取文件内容的几种方法
1.fread string fread ( int $handle , int $length ) fread() 从 handle 指向的文件中读取最多 length 个字节.该函数在读取完最多 ...
- API读取和处理的文件
1.FileList对象 FileList对象是File对象的一个集合,设置multiple就可以多文件上传.2.Blob对象 Blob对象就是一个二进制原始数据对象,它提供了slice方法可以读取 ...
- windows7下python3.4.3 添加库路径(转)
1, 动态的添加库路径.在程序运行过程中修改sys.path的值,添加自己的库路径import syssys.path.append(r'your_path') 2, 在Python安装目录下的\Li ...
- 使用Spring + Jedis集成Redis
转自:http://my.oschina.net/u/866380/blog/521658 摘要 使用Spring和Jedis完成分片Redis的集成 一.集成环境 Tomcat7 JDK1.7 Je ...
- 转:SQL:外连接on条件与where条件的区别
原文地址:http://hi.baidu.com/benben1006/blog/item/187deb77bc0e5319b151b974.html 数据库在通过连接两张或多张表来返回记录时,都会生 ...
- JAVA常用运算符
Java 语言中常用的运算符可分为如下几种: 1.算术运算符 int i = 5; int j = i++; // i = 6 j = 5 int j = ++i; // i = 6 j = 6 PS ...
- Win10 EPLAN新建项目出现“一个内部错误的解决方法”
[环境] Win10 64bits,EPLAN 2.4 64bits. [表现] 新建项目的时候出现"一个内部错误"的提示,然后软件卡死. [解决方案] 计算机管理--服务--EP ...
- about JNI
1.Java对C/C++事件处理的封装JIT(Just in Time.Java语言的原动态编译技术) 大多数的游戏引擎都是使用可移植的C语言开发的,然后通过简单的封装以适应特殊的平台. 2.Andr ...
- Scrum 项目3.0
Scrum 项目3.0 3.0----------------------------------------------------- SCRUM 流程的步骤2: Spring 计划 1. 确保pr ...
- php pdo错误:SQLSTATE[HY093]: Invalid parameter number: parameter was not defined
原因:在使用execute()执行时没有传对应prepare()设置的参数