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 ...
随机推荐
- 行为识别笔记:improved dense trajectories算法(iDT算法)(转载)
iDT算法是行为识别领域中非常经典的一种算法,在深度学习应用于该领域前也是效果最好的算法.由INRIA的IEAR实验室于2013年发表于ICCV.目前基于深度学习的行为识别算法效果已经超过了iDT算法 ...
- request获取json
$.ajax({ type:'post', url:'${ctx}/recordServiceController/queryDetail.do', //contentType:'applicatio ...
- PHP迭代
计算1到10的和: <?php function f($n) { $before_1 = 1; $before_2 = 1; for( $i = 3;$i <= $n;++$i ) { $ ...
- Too many authentic authentication failures for root
连接SSH服务器时报 Too many authentic authentication failures for root. 方法一:删除目标服务器 /root/.ssh/目录下的 known_ho ...
- Analyzing The Papers Behind Facebook's Computer Vision Approach
Analyzing The Papers Behind Facebook's Computer Vision Approach Introduction You know that company c ...
- 论文笔记之:Deep Reinforcement Learning with Double Q-learning
Deep Reinforcement Learning with Double Q-learning Google DeepMind Abstract 主流的 Q-learning 算法过高的估计在特 ...
- C语言 文件读取
FILE *fp = fopen("data.txt","rt");fscanf(fp,"%d", &n ); /* 把数据放到数组 ...
- Python_Day6_反射_正则表达式之篇
一.反射 定义:利用字符串形式去对象(模块)中操作(寻找/检查/删除/设置)成员 #getattr:获取模块中属性 #hasattr:检查模块中是否存在某个成员(函数) #delattr:删除模块中成 ...
- Python画图形界面
使用QTdesigner 生成.ui文件,输入命令行pyuic4 -o test.py test.ui 在生成的Python文件后面输入下面代码 if __name__=="__main__ ...
- ant脚本编写
使用ant脚本前的准备 1.下载一个ant安装包.如:apache-ant-1.8.4-bin.zip.解压到E盘. 2.配置环境变量.新增ANT_HOME:E:\apache-ant-1.8.4:P ...