Leetcode 0025. Reverse Nodes in k-Group
居然把头插法写错了,debug了一个多小时
/**
* 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 == || head == nullptr) return head;
ListNode *tail = head;
int i;
for(i=;i<k && tail != nullptr;i++,tail=tail->next);
if(i<k) return head;
tail = reverseKGroup(tail, k);
ListNode* rear = head,*tmp=nullptr;
head= tail;
for(int i = ;i<k;i++){
tmp = rear->next;
rear->next = head;
head = rear;
rear = tmp;
}
return head;
}
};
Leetcode 0025. 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 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
Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked list k at a time and ret ...
- 蜗牛慢慢爬 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 025 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 an ...
- [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 ...
随机推荐
- Objective-C中nil与release的区别与用法
首先说一下他们两的作用,nil就是把一个对象的指针置为空,只是切断了指针与内存中对象的联系,它对内存的释放没有什么作用:而release才是真正用于内存释放的,release后系统会将该块内存标记为可 ...
- git(5) windows下 pycharm + git(github) ,在本地方便管理
本篇博客讲解一下,windows下如何在pycharm下使用git(使用github设置和git一样),在本地进行commit,push,pull等操作 优点:简单,方便 pycharm版本:5.0. ...
- file_get_content和curl的性能比较
今天在获取微信一张二维码图片时发现使用php中的file_get_content方式和curl方式竟然相差了50倍左右,直接晕倒!!!
- Linq常用操作
http://www.cnblogs.com/knowledgesea/p/3897665.html
- Win7玩游戏偶尔自动跳转到桌面的解决办法[转]
新装的win7旗舰版SP1,怎么玩wow (魔兽世界.极品飞车.全屏游戏.按键精灵.挂机)总是过一会就自己返回桌面了.刚开始以为是显卡的毛病,更新了驱动还是一样(在这之前,排除病毒,其他驱动问题).因 ...
- 第5章 Posix 消息队列
5.1 概述 消息队列可以认为是一个链表.有写权限的线程可往消息队列中放置消息,有读权限的线程可以从消息队列中取走消息. 消息队列和管道/FIFO的区别: (1)消息队列往一个队列中写消息前,并不需要 ...
- CRM plugin 激活 停用 事件
需要注册 SetState 和 SetStateDynamecEntity
- 山东省第三届ACM省赛
Solved ID PID Title Accepted Submit A 2407 Impasse (+) 0 0 B 2415 Chess 0 0 C 2414 An interest ...
- POJ 1286 【POLYA】
题意: 给你三种颜色的珠子,每次给你N,问在旋转,翻转之后视作相同的情况下,能组成多少种不同的项链. 思路: 让我们借这道题拯救一下我对POLYA定理的理解... sigma(m^(gcd(i,n)) ...
- SparseArray,dip & px
SparseArray-用Array的方式实现Integer-Object的map 优:节约内存,因为避免了装箱/拆箱,数据结构不依赖Entry 劣:速度不及HashMap dip.px dip(de ...