【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 ...
随机推荐
- Asp.Net Core App 部署故障示例 2
相关阅读:Windows + IIS 环境部署Asp.Net Core App 1. HTTP Error 502.5 – Process Failure 环境 Windows Server 201 ...
- Kmeans聚类算法分析(转帖)
原帖地址:http://www.opencvchina.com/thread-749-1-1.html k-means是一种聚类算法,这种算法是依赖于点的邻域来决定哪些点应该分在一个组中. ...
- go语言基础之append函数的使用
1.append函数的使用 作用:在原切片的末尾添加元素 示例: package main //必须有个main包 import "fmt" func main() { s1 := ...
- js实现trim() JS去掉首尾空格 JS去掉两头空格
function trimStr(str){ return str.replace(/(^\s*)|(\s*$)/g,""); } 用的时候就是直接 var 变量=trimStr( ...
- Android开发之Drag&Drop框架实现拖放手势
Android3.0提供了drag/drop框架,利用此框架可以实现使用拖放手势将一个view拖放到当前布局中的另外一个view中.本文将介绍如何使用拖放框架. 一.实现拖放的步骤 首先,我们先了解一 ...
- 使用sed进行文字替换
范式: sed -i "s/查找内容/替换后内容/g" `grep 查找内容 -rl 查找开始路径` 例子: #sed -i "s/abc/ABC/g" `gr ...
- 整理两个PetaPoco连接SQLite数据库的方法
从https://github.com/qingask/PetaPoco.NetCore下载源文件压缩包 解压出文件PetaPoco.Multiple.cs.PetaPoco.NetCore.cs 放 ...
- VM虚拟机安装之后出现无法自动登录到桌面以及__vmware_user__怎么办
1 运行control userpasswords2 打开用户账户对话框,点击高级选项中的"高级"按钮 2 右击" __vmware_user__"这个账户,选 ...
- Python网络编程 - 请求地址上的文件并下载
我们用到了requests库,由于是第三方的,必须下载 如果是python 2.x用下面命令 pip install requests python 3.x用下面命令 easy_install req ...
- Linux对文件归档和压缩(学习笔记八)
一.归档和压缩 压缩命令工具:gzip,bzip2 归档命令工具:tar 二.压缩 2.1.gzip gzip是一种标准的.广泛应用的文件压缩和解压缩实用工具.gzip允许文件并置.用gzip压缩文件 ...