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 ...
随机推荐
- int除以int 得到double类型值
double serviceability =(double)(count1+count2)/sum; 需要进行强转 除数 这样得到的值就是double类型了
- @Transactional注解*
类或者方法加@Transactional注解 表示该类里面的所有方法或者这个方法的事务由spring处理,来保证事务的原子性,不知道这样说你能不能理解,即是方法里面对数据库操作,如果有一个方法操作失败 ...
- JavaWeb学习记录(二十五)——权限管理总结
一.面向对象思想简化数据库操作 public List<Role> getObjectsByIds(List<AdminRole> adminRoles) { L ...
- hadoop常用管理员命令
hadoop job -list 列出正在运行的job hadoop job -kill kill掉job hadoop fsck 检查HDFS坏快 hadoop dfsadmin -report检查 ...
- UVA-11235 Frequent values (RMQ)
题目大意:在一个长度为n的不降序列中,有m次询问,每次询问(i,j)表示在区间(i,j)中找出出现次数最多的元素的出现次数. 题目分析:因为序列有序,可以将序列分段,并且记录每段的元素个数.每一个元素 ...
- 让PHP代码更危险----使用别的系统命令--如sql语句--exec(),system()方法甚至html的js语句
如题,所以涉及到别的语言时,程序就可能更加不安全.
- Oracle升级前备份和失败回退
一.升级前备份 1.软件备份[root@localhost backup]# su - oracle [oracle@localhost ~]$ cd $ORACLE_HOME[oracle@loca ...
- RxJava + Retrofit 的实际应用场景
关于 RxJava Retrofit 很多篇文章都有详细的说明,在这里我想分享一个具体的使用案例,在我的开源项目 就看天气 里的实际应用.也希望跟大家探讨如何优雅的使用. 准备 项目中用到的依赖: c ...
- s2-032批量脚本
看乌云上许多大牛上脚本,我也写个玩吧!写的比较简单.懒得优化,参数获取就自己改吧 需要抓很多struts,可用爱站工具包或则自己写个脚本爬 #coding:utf8 import urllib2 im ...
- nmap与ntop
http://blog.csdn.net/aspirationflow/article/details/7694274