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. linux常用C函数目录

    字符测试篇 isalnum isalpha isascii iscntrl isdigit isgraphis islower isprint isspace ispunct isupper isxd ...

  2. C/C++嵌入式开发面试题

    C/C++嵌入式开发面试题 预处理器(Preprocessor) 1. 用预处理指令#define 声明一个常数,用以表明1年中有多少秒(忽略闰年问题) #define SECONDS_PER_YEA ...

  3. UI_UITabBarController

    建立控制器 // 普通控制器 GroupViewController *groupVC = [[GroupViewController alloc] init]; SecondViewControll ...

  4. C语言:返回两个数组中第一个元素的指针,并输出这个值

    // //  main.c //  Pointer_search // //  Created by ma c on 15/8/2. //  Copyright (c) 2015年. All righ ...

  5. UNdelete

    --90兼容模式以上,2005+ -- http://raresql.com/2012/10/24/sql-server-how-to-find-who-deleted-what-records-at ...

  6. IE浏览器无法直接识别input的type="hidden"问题

    原问题: <td class="formValue" id="in-checkbox"> <label class="checkbo ...

  7. 在CentOS/RHEL上设置SSH免密码登录

    本文会告诉你怎样在 CentOS/RHEL 上设置 SSH 免密码登录.自动登录配置好以后,你可以通过它使用 SSH (Secure Shell)和安全复制 (SCP)来移动文件. SSH 是开源的, ...

  8. Hyper-V如何应用新的网卡

    最近新装了块网卡,可是在Hyper-V的虚拟机设置里怎么也找不到如何应用这个新网卡.   把我郁闷坏了. 偶尔点点,才发现原来不是在虚拟机的设置里面,而是在上面的一级设置. 新建完后就可以在虚拟机的网 ...

  9. [Node.js]30. Level 6: Listen 'Question' from client, and then Answer the Question

    Clients can also answer each other questions, so let's build that feature by first listening for the ...

  10. hibernate4.3.10环境搭建

    1.首先还是引入所须要的包 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFC ...