【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 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个结点进栈,再出栈,就实现了逆序。
/**
* 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) {
ListNode* newhead = new ListNode(-);
ListNode* tail = newhead;
ListNode* begin = head;
ListNode* end = begin;
while(true)
{
int count = k;
while(count && end != NULL)
{
end = end->next;
count --;
}
if(count == )
{//reverse from [begin, end)
stack<ListNode*> s;
while(begin != end)
{
s.push(begin);
begin = begin->next;
}
while(!s.empty())
{
ListNode* top = s.top();
s.pop();
tail->next = top;
tail = tail->next;
}
}
else
{//leave out
tail->next = begin;
break;
}
}
return newhead->next;
}
};

解法二:
自定义函数reverse(begin, end)
对[begin, end]范围内实现逆序,并且更新begin, end
逐k次调用即可。
注意:
(1)由于需要更新begin, end,因此参数形式为ListNode*&
(2)在[begin,end]范围内实现逆序之后,需要链如原先的链表,不可脱离
/**
* 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(head == NULL)
return NULL;
if(k == )
//no swap
return head; int i = ;
//head node
ListNode* newhead = new ListNode(-);
newhead->next = head;
ListNode* tail = newhead; ListNode* begin = head;
ListNode* end = begin;
while(end != NULL)
{
if(i%k == )
{
reverse(begin, end);
tail->next = begin;
tail = end;
//new begin
begin = end->next;
}
end = end->next;
i ++;
}
return newhead->next;
}
void reverse(ListNode*& begin, ListNode*& end)
{//reverse the list. begin points to new begin, end points to new end
if(begin == end)
{//only one node
return;
}
else if(begin->next == end)
{//two nodes
begin->next = end->next;
end->next = begin;
//swap begin and end
ListNode* temp = begin;
begin = end;
end = temp;
}
else
{//at least three nodes
ListNode* pre = begin;
ListNode* cur = pre->next;
ListNode* post = cur->next; while(post != end->next)
{
cur->next = pre;
pre = cur;
cur = post;
post = post->next;
}
cur->next = pre;
//old begin points to the new end
end = begin;
end->next = post;
//cur points to the old end
begin = cur;
}
}
};

【LeetCode】25. Reverse Nodes in k-Group (2 solutions)的更多相关文章
- 【LeetCode】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. k ...
- 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)
[LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
- 【一天一道LeetCode】#25. Reverse Nodes in k-Group
一天一道LeetCode系列 (一)题目 Given a linked list, reverse the nodes of a linked list k at a time and return ...
- 【LeetCode】025. 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. k ...
- 【leetcode】557. Reverse Words in a String III
Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...
- [Leetcode][Python]25: Reverse Nodes in k-Group
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 25: Reverse Nodes in k-Grouphttps://oj. ...
- 【LeetCode】151. Reverse Words in a String
Difficulty: Medium More:[目录]LeetCode Java实现 Description Given an input string, reverse the string w ...
- 【LeetCode】#7 Reverse Integer
[Question] Reverse digits of an integer. Example: x = 123, return 321 x = -123, return -321 [My Solu ...
- 【LeetCode】24. Swap Nodes in Pairs (3 solutions)
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...
随机推荐
- 【BZOJ】【2878】【NOI2012】迷失游乐园
树形+基环树DP/数学期望 然而我并不会做…… 题解戳这里:http://blog.csdn.net/u011265346/article/details/46328543 好吧先考虑一个简单点的,当 ...
- Android几种Service常驻内存的小思路
老话说的好:躲得了初一,躲只是高三 ! 大多数的Android开发人员遇到的一个问题-怎样保证Service常驻内存. 近期我最终也在项目中务必幸运的遇到了 先来了解一下什么是Service常驻内存. ...
- Android Studio体验(一)--Window版本安装
如果说之前看见有人用Android Studio你还是不屑一顾的话,那么现在该改变态度了,正如我一样,之前一直习惯于Android内置ADT插件的捆绑Ecliple,现在Android Studio发 ...
- Android视频播放-SurfaceView和Mediaplayer
好几天没写博客了,处理了一点个人私事加上平时加班,基本上时间不充裕,上篇文章讲了一下用Mediaplayer来播放音乐,这次就讲讲使用Mediaplayer来和SurfaceView配合播放一个视频流 ...
- mongoDB报错Cannot find module '../build/Release/bson'
打算用nodejs写一个blog系统,发现nodejs还是存在很多的坑.在使用mongodb时遇到如下报错问题: { [Error: Cannot find module '../build/Rele ...
- Inside GDALAllRegister之二: 自动加载驱动
代码 GetGDALDriverManager()->AutoLoadDrivers(); 包含了两部分: 首先获得GDALDriverManager的singleton对象的指针,这点之 ...
- .NET/Mysql-petatoco连接mysql数据库
安装mysql数据库 用nugget添加.net连接mysql数据库的组件
- [Backbone] Verying Views
Below we have our AppointmentsView instance rendering and then taking the rendered HTML and insertin ...
- 【Nodejs】nimble或async并不能保证程序串行执行,回调是回避不了的坑
先看一段例程: //------------------------------- // 用于创建目录 //------------------------------- function creat ...
- PHP高级教程-多维数组
PHP 多维数组 一个数组中的值可以是另一个数组,另一个数组的值也可以是一个数组.依照这种方式,我们可以创建二维或者三维数组: 实例 <?php // 二维数组: $cars = array ( ...