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)的更多相关文章

  1. 【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  ...

  2. 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)

    [LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

  3. 【一天一道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 ...

  4. 【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  ...

  5. 【leetcode】557. Reverse Words in a String III

    Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...

  6. [Leetcode][Python]25: Reverse Nodes in k-Group

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 25: Reverse Nodes in k-Grouphttps://oj. ...

  7. 【LeetCode】151. Reverse Words in a String

    Difficulty: Medium  More:[目录]LeetCode Java实现 Description Given an input string, reverse the string w ...

  8. 【LeetCode】#7 Reverse Integer

    [Question] Reverse digits of an integer. Example: x = 123, return 321 x = -123, return -321 [My Solu ...

  9. 【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 ...

随机推荐

  1. [Swift] Swift3.0--GCD

    reference to : http://www.jianshu.com/p/4c983388dca6 估计现在好多人在为这一块头疼,所以先来点干货. //最常用模板 //全局队列异步执行 Disp ...

  2. Flask 学习(四)静态文件

    Flask 学习(四)静态文件 动态 web 应用也需要静态文件,一般是 CSS 和 JavaScript 文件.理想情况下你的服务器已经配置好提供静态文件的服务. 在开发过程中, Flask 也能做 ...

  3. Okhttp【简介】应用 示例

    资源 GitHub:https://github.com/square/okhttp 官网     文档     API  You'll also need Okio[https://github.c ...

  4. Open edX 学习、开发、运维相关链接整理

    原文地址:http://edustack.org/ 所需知识: Linux Git Python (Django Mako coffeescript sass) (MongoDB Mysql) Ans ...

  5. IIS7.5配置Asp.net项目出现HTTP 错误 404.17 - Not Found 请求的内容似乎是脚本,因而将无法由静态文件处理程序来处理。

    近日在将一个Asp.net项目部署到IIS7.5上时却出现了HTTP 错误 404.17 - Not Found 请求的内容似乎是脚本,因而将无法由静态文件处理程序来处理. 因为IIS里面使用的都是默 ...

  6. 如何利用Framework模型生成IQD文件

    很多Cognos的新手在接触Transform建模的时候对于iqd文件都有一种朦胧的感觉,当然也不必去死记硬别它的格式,下面我们就来说一下如何用Framework工具来生成iqd文件. 1:打开fra ...

  7. 内容匹配广告投放技术4:网盟CTR预估(百度文库课程)

    原文:http://wbj0110.iteye.com/blog/2043065 该文是百度文库课程<计算广告学之内容匹配广告&展示广告原理.技术和实践>的课程笔记,感谢百度! 课 ...

  8. loadicon后一定要调用destroyicon吗

    Remarks It is only necessary to call DestroyIcon for icons and cursors created with the following fu ...

  9. Android直播实现 Android端推流、播放

    最近想实现一个Android直播,但是对于这方面的资料都比较零碎,一开始是打算用ffmpeg来实现编码推流,在搜集资料期间,找到了几个强大的开源库,直接避免了jni的代码,集成后只用少量的java代码 ...

  10. Faiss学习:一

    在多个GPU上运行Faiss以及性能测试 一.Faiss的基本使用 1.1在CPU上运行 Faiss的所有算法都是围绕index展开的.不管运行搜索还是聚类,首先都要建立一个index. import ...