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. Leader之重

    1:合理安排每个CASE并检查每个人每天的工作进度和质量: 这会让一个庞大的工作,或者看上不可能完成的任务,变成可完成的.   2:警惕对立情绪,并寻找交接者: 永远无法控制所有成员对你或者对团队对公 ...

  2. #line 的作用是改变当前行数和文件名称

    #line 的作用是改变当前行数和文件名称,它们是在编译程序中预先定义的标识符命令的基本形式如下:   #line number["filename"]其中[]内的文件名可以省略. ...

  3. 附4 springboot源码解析-run()

    public ConfigurableApplicationContext run(String... args) { StopWatch stopWatch = new StopWatch(); / ...

  4. C#中的集合(HashTable与Array类)

    一.Array类 1.Array类的属性 序号 属性 & 描述 1 IsFixedSize 获取一个值,该值指示数组是否带有固定大小. 2 IsReadOnly 获取一个值,该值指示数组是否只 ...

  5. 配置sonarqube+maven

    Maven与Sonar配合使用  准备工作:下载sonarqube源码即可  步骤:      1).安装sonar 解压,启动sonarqube-4.1\bin\windows-x86-32目录下的 ...

  6. C#一个FTP操作封装类FTPHelper

    参考了网上一些代码,作了一些调整优化. 001 using System; 002 using System.Collections.Generic; 003 using System.Linq; 0 ...

  7. IOS之导航控制器

    UINavigationController是用于构建分层应用程序的主要工具,主要采用栈形式来实现视图.任何类型的视图控制器都可放入栈中.在设计导航控制器时需要指定根视图即用户看到的第一个视图.根视图 ...

  8. Volley框架的介绍使用

    Volley是在2013年的Google I/O 2013大会上发布的,是我们的网络通信更快,更简单,更方便.对于初学者来讲是一个很好的框架. 简单来说,它提供了如下的便利功能: JSON,图像等的异 ...

  9. idea丢失svn解决办法

    今天打开Idea,习惯用ctrl+t来更新svn,杯具出现了,快捷键失效了,我觉得可能是其他的什么软件占用了这个快捷键,于是把qq,微信,rtx,各种软件都关掉,发现还是不好使,于是重启了一下,发现还 ...

  10. (算法)位图BitMap

    题目: 给定一数组,大小为M,数组中的数字范围为1-N,如果某带宽有限,无法传输该大小的数组,该怎么办? 思路: 通过位图BitMap来压缩数组,将数组中每个数字在bit位上标志,这样就可以将数组大小 ...