[LeetCode] 25. Reverse Nodes in k-Group 每k个一组翻转链表
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
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
Note:
- Only constant extra memory is allowed.
- You may not alter the values in the list's nodes, only nodes itself may be changed.
这道题让我们以每k个为一组来翻转链表,实际上是把原链表分成若干小段,然后分别对其进行翻转,那么肯定总共需要两个函数,一个是用来分段的,一个是用来翻转的,以题目中给的例子来看,对于给定链表 1->2->3->4->5,一般在处理链表问题时,大多时候都会在开头再加一个 dummy node,因为翻转链表时头结点可能会变化,为了记录当前最新的头结点的位置而引入的 dummy node,加入 dummy node 后的链表变为 -1->1->2->3->4->5,如果k为3的话,目标是将 1,2,3 翻转一下,那么需要一些指针,pre 和 next 分别指向要翻转的链表的前后的位置,然后翻转后 pre 的位置更新到如下新的位置:
- -->->->->->
- | | |
- pre cur next
- -->->->->->
- | | |
- cur pre next
以此类推,只要 cur 走过k个节点,那么 next 就是 cur->next,就可以调用翻转函数来进行局部翻转了,注意翻转之后新的 cur 和 pre 的位置都不同了,那么翻转之后,cur 应该更新为 pre->next,而如果不需要翻转的话,cur 更新为 cur->next,代码如下所示:
解法一:
- class Solution {
- public:
- ListNode* reverseKGroup(ListNode* head, int k) {
- if (!head || k == ) return head;
- ListNode *dummy = new ListNode(-), *pre = dummy, *cur = head;
- dummy->next = head;
- for (int i = ; cur; ++i) {
- if (i % k == ) {
- pre = reverseOneGroup(pre, cur->next);
- cur = pre->next;
- } else {
- cur = cur->next;
- }
- }
- return dummy->next;
- }
- ListNode* reverseOneGroup(ListNode* pre, ListNode* next) {
- ListNode *last = pre->next, *cur = last->next;
- while(cur != next) {
- last->next = cur->next;
- cur->next = pre->next;
- pre->next = cur;
- cur = last->next;
- }
- return last;
- }
- };
我们也可以在一个函数中完成,首先遍历整个链表,统计出链表的长度,然后如果长度大于等于k,交换节点,当 k=2 时,每段只需要交换一次,当 k=3 时,每段需要交换2此,所以i从1开始循环,注意交换一段后更新 pre 指针,然后 num 自减k,直到 num<k 时循环结束,参见代码如下:
解法二:
- class Solution {
- public:
- ListNode* reverseKGroup(ListNode* head, int k) {
- ListNode *dummy = new ListNode(-), *pre = dummy, *cur = pre;
- dummy->next = head;
- int num = ;
- while (cur = cur->next) ++num;
- while (num >= k) {
- cur = pre->next;
- for (int i = ; i < k; ++i) {
- ListNode *t = cur->next;
- cur->next = t->next;
- t->next = pre->next;
- pre->next = t;
- }
- pre = cur;
- num -= k;
- }
- return dummy->next;
- }
- };
我们也可以使用递归来做,用 head 记录每段的开始位置,cur 记录结束位置的下一个节点,然后调用 reverse 函数来将这段翻转,然后得到一个 new_head,原来的 head 就变成了末尾,这时候后面接上递归调用下一段得到的新节点,返回 new_head 即可,参见代码如下:
解法三:
- class Solution {
- public:
- ListNode* reverseKGroup(ListNode* head, int k) {
- ListNode *cur = head;
- for (int i = ; i < k; ++i) {
- if (!cur) return head;
- cur = cur->next;
- }
- ListNode *new_head = reverse(head, cur);
- head->next = reverseKGroup(cur, k);
- return new_head;
- }
- ListNode* reverse(ListNode* head, ListNode* tail) {
- ListNode *pre = tail;
- while (head != tail) {
- ListNode *t = head->next;
- head->next = pre;
- pre = head;
- head = t;
- }
- return pre;
- }
- };
Github 同步地址:
https://github.com/grandyang/leetcode/issues/25
类似题目:
参考资料:
https://leetcode.com/problems/reverse-nodes-in-k-group/
https://leetcode.com/problems/reverse-nodes-in-k-group/discuss/11435/C%2B%2B-Elegant-and-Small
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] 25. Reverse Nodes in k-Group 每k个一组翻转链表的更多相关文章
- Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表)
Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表) 题目描述 已知一个链表,每次对k个节点进行反转,最后返回反转后的链表 测试样例 Inpu ...
- [leetcode]25. Reverse Nodes in k-Group每k个节点反转一下
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k ...
- LeetCode 25 Reverse Nodes in k-Group Add to List (划分list为k组)
题目链接: https://leetcode.com/problems/reverse-nodes-in-k-group/?tab=Description Problem :将一个有序list划分 ...
- [LeetCode]25. Reverse Nodes in k-Group k个一组翻转链表
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k ...
- [leetcode 25]Reverse Nodes in k-Group
1 题目: Given a linked list, reverse the nodes of a linked list k at a time and return its modified li ...
- Java [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 li ...
- leetcode:Reverse Nodes in k-Group(以k为循环节反转链表)【面试算法题】
题目: Given a linked list, reverse the nodes of a linked list k at a time and return its modified list ...
- 蜗牛慢慢爬 LeetCode 25. Reverse Nodes in k-Group [Difficulty: Hard]
题目 Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. ...
- [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 ...
随机推荐
- 【Java面试题】short s1 = 1; s1 = s1 + 1;有错吗?short s1 = 1; s1 += 1;有错吗?
昨天去面试,虽然体验不是很好, 但是看到了这个面试题,当时感觉无从下手,所以在这里记录一下. 解决这道题之前,先复习一下Java的基本数据类型转换规则,以便后面对面试题的理解. java的基本数据类型 ...
- 搜索旋转排序数组II
题目 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [,,,,,,] 可能变为 [,,,,,,] ). 编写一个函数来判断给定的目标值是否存在于数组中.若存在返回 true, ...
- kali渗透综合靶机(十)--Raven靶机
kali渗透综合靶机(十)--Raven靶机 一.主机发现 1.netdiscover -i eth0 -r 192.168.10.0/24 二.端口扫描 1. masscan --rate=1000 ...
- 使用 Xbox Game 录制桌面视频(录制音频)
使用 Xbox Game 录制桌面视频(附带音频) 前言:可能自己音频输出的问题,一直无法用工具录制桌面的音频,而最后发现利用 Xbox Game 录制游戏视频的功能很好地解决我们的问题. 1)打开游 ...
- 时间复杂度o(1), o(n), o(logn), o(nlogn)
1.时间复杂度o(1), o(n), o(logn), o(nlogn).算法时间复杂度的时候有说o(1), o(n), o(logn), o(nlogn),这是算法的时空复杂度的表示.不仅仅用于表示 ...
- C# NuGet常用命令
命令执行位置:工具=〉Nuget包管理器=〉程序包管理器控制台 一.安装 1.安装指定版本类库install-package <程序包名> -version <版本号> 2.安 ...
- Jenkins配置LDAP认证
managerdn即为连接到AD的账号
- Python requests库的使用(二)
1.请求异常处理 请求异常类型: 请求超时处理(timeout): 实现代码: import requestsfrom requests import exceptions #引入exc ...
- Java自学-集合框架 Collections
Java集合框架 工具类Collections Collections是一个类,容器的工具类,就如同Arrays是数组的工具类 步骤 1 : 反转 reverse 使List中的数据发生翻转 pack ...
- uni-app学习(五)好用的插件3
1. uni-app学习(五)好用的插件3 1.1. 分享推广页面 分享推广页面,分享第三方.保存二维码.复制推广地址 模板地址 示例 这个用到的几率还是蛮大的,可以直接拿来修改下用 1.2. 教育A ...