【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 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.
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个数可以反转,如果没有k个数则返回头结点,否则反转该部分链表并递归求解后来的反转链表部分。为数不多的Hard 题AC
Solution 1
/**
* 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* cur = head, *tail = nullptr;
for(int i = ; i < k; ++i) {
if (!cur)
return head;
if (i == k - )
tail = cur;
cur = cur->next;
} reverse(head, tail);
head->next = reverseKGroup(cur, k); return tail;
} void reverse(ListNode* head, ListNode* tail) {
if (head == tail)
return;
ListNode* pre = head, *cur = pre->next;
while(cur != tail) {
ListNode* tmp = cur->next;
cur->next = pre;
pre = cur;
cur = tmp;
}
cur->next = pre;
}
};
简化版:
 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;
     }
 };
转自:Grandyang
首先遍历整个链表确定总长度,如果长度大于等于k,那么开始反转这k个数组成的部分链表。交换次数为k-1次,反转后,更新剩下的总长度,迭代进行。
Solution 2
/**
* 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 dummy(-);
dummy.next = head;
ListNode* pre = &dummy, *cur = pre;
int len = ;
while (cur->next) {
++len;
cur = cur->next;
}
while (len >= k) {
cur = pre->next;
for (int i = ; i < k - ; ++i) {
ListNode* tmp = cur->next;
cur->next = tmp->next;
tmp->next = pre->next;
pre->next = tmp;
}
pre = cur;
len -= k;
} return dummy.next;
}
};
【LeetCode】025. Reverse Nodes in k-Group的更多相关文章
- 【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 (2 solutions)
		
Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked list k at a time and ret ...
 - 【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】557. Reverse Words in a String III
		
Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...
 - 【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 ...
 - [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. If ...
 - 【LeetCode】541. Reverse String II 解题报告(Python)
		
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
 
随机推荐
- 版本控制系统Subversion
			
系统提供撤销的功能对我们实际开发中特别重要.改动后撤销几乎也是我们每个人经常做的事情.再多人进行同一个项目的开发或者测试的时候,版本的唯一性(类似于临界区资源),也就是说A 和B 两个人协同工作的时候 ...
 - 用matlab将nc数据读出来,写成二进制文件,然后用grads画图
			
clear,clc nt=735;ny=73; %2.5*2.5格点的nx=144; %2.5*2.5格点的f=netcdf('air.mon.mean.nc','nowrite');tt ...
 - uCOS-II的学习笔记(共九期)和例子(共六个)
			
源:uCOS-II的学习笔记(共九期)和例子(共六个) 第一篇 :学习UCOS前的准备工作http://blog.sina.com.cn/s/blog_98ee3a930100w0eu.html 第二 ...
 - [SCOI2003]蜘蛛难题
			
题目 对于当年来说似乎是神题?? 做法 对于联通注水来说,我们考虑把所有能平分到水的桶同时加高度,然后暴力判断 My complete code copy来的代码 #include <cstdi ...
 - Unity Json 之三
			
今天在网上看到一个simplejson,直接调用这两个API就可以了,简单易用 string jsonstr = SimpleJson.SimpleJson.SerializeObject(json) ...
 - INSPIRED启示录 读书笔记 - 第41章 产品经理的反省清单
			
十大问题 1.产品能吸引目标消费者的关注吗? 2.产品的设计是否人性化,是否易于操作? 3.产品能在竞争中取胜吗?即使是面对未来风云变化的市场,依旧有取胜的把握吗? 4.我了解目标用户吗?产品(不是理 ...
 - 使用 Apache Spark 让 MySQL 查询速度提升 10 倍以上
			
转: https://coyee.com/article/11012-how-apache-spark-makes-your-slow-mysql-queries-10x-faster-or-more ...
 - java进阶之-Maven,svn,git,maven合拼多个项目
			
git的使用介绍(写很容易懂得哦) maven合拼多个项目(写得很好哦) MAVEN作用:统一开发规范与工具:统一管理jar包 1.下载MAVEN 下载绿色版的面安装 2.环境配置 eclipse想 ...
 - MSER最稳定极值区域源码分析
			
最稳定极值区域介绍 如把灰度图看成高低起伏的地形图,其中灰度值看成海平面高度的话,MSER的作用就是在灰度图中找到符合条件的坑洼.条件为坑的最小高度,坑的大小,坑的倾斜程度,坑中如果已有小坑时大坑与小 ...
 - ZooKeeper服务-数据模型
			
ZooKeeper是一个具有高可用性的高性能协调服务. 数据模型 ZooKeeper维护着一个树形层次结构,树中的节点被称为znode.Znode可以用于存储数据,并且有一个与之相关联的ACL(Acc ...