mirror of
https://github.com/labuladong/fucking-algorithm.git
synced 2025-07-05 03:36:39 +08:00
Merge pull request #99 from Justin-YGG/english
translate 高频面试系列/k个一组反转链表.md
This commit is contained in:
@ -1,131 +0,0 @@
|
||||
# 如何k个一组反转链表
|
||||
|
||||
之前的文章「递归反转链表的一部分」讲了如何递归地反转一部分链表,有读者就问如何迭代地反转链表,这篇文章解决的问题也需要反转链表的函数,我们不妨就用迭代方式来解决。
|
||||
|
||||
本文要解决「K 个一组反转链表」,不难理解:
|
||||
|
||||

|
||||
|
||||
这个问题经常在面经中看到,而且 LeetCode 上难度是 Hard,它真的有那么难吗?
|
||||
|
||||
对于基本数据结构的算法问题其实都不难,只要结合特点一点点拆解分析,一般都没啥难点。下面我们就来拆解一下这个问题。
|
||||
|
||||
### 一、分析问题
|
||||
|
||||
首先,前文[学习数据结构的框架思维](../算法思维系列/学习数据结构和算法的框架思维.md)提到过,链表是一种兼具递归和迭代性质的数据结构,认真思考一下可以发现**这个问题具有递归性质**。
|
||||
|
||||
什么叫递归性质?直接上图理解,比如说我们对这个链表调用 `reverseKGroup(head, 2)`,即以 2 个节点为一组反转链表:
|
||||
|
||||

|
||||
|
||||
如果我设法把前 2 个节点反转,那么后面的那些节点怎么处理?后面的这些节点也是一条链表,而且规模(长度)比原来这条链表小,这就叫**子问题**。
|
||||
|
||||

|
||||
|
||||
我们可以直接递归调用 `reverseKGroup(cur, 2)`,因为子问题和原问题的结构完全相同,这就是所谓的递归性质。
|
||||
|
||||
发现了递归性质,就可以得到大致的算法流程:
|
||||
|
||||
**1、先反转以 `head` 开头的 `k` 个元素**。
|
||||
|
||||

|
||||
|
||||
**2、将第 `k + 1` 个元素作为 `head` 递归调用 `reverseKGroup` 函数**。
|
||||
|
||||

|
||||
|
||||
**3、将上述两个过程的结果连接起来**。
|
||||
|
||||

|
||||
|
||||
整体思路就是这样了,最后一点值得注意的是,递归函数都有个 base case,对于这个问题是什么呢?
|
||||
|
||||
题目说了,如果最后的元素不足 `k` 个,就保持不变。这就是 base case,待会会在代码里体现。
|
||||
|
||||
### 二、代码实现
|
||||
|
||||
首先,我们要实现一个 `reverse` 函数反转一个区间之内的元素。在此之前我们再简化一下,给定链表头结点,如何反转整个链表?
|
||||
|
||||
```java
|
||||
// 反转以 a 为头结点的链表
|
||||
ListNode reverse(ListNode a) {
|
||||
ListNode pre, cur, nxt;
|
||||
pre = null; cur = a; nxt = a;
|
||||
while (cur != null) {
|
||||
nxt = cur.next;
|
||||
// 逐个结点反转
|
||||
cur.next = pre;
|
||||
// 更新指针位置
|
||||
pre = cur;
|
||||
cur = nxt;
|
||||
}
|
||||
// 返回反转后的头结点
|
||||
return pre;
|
||||
}
|
||||
```
|
||||
|
||||

|
||||
|
||||
这次使用迭代思路来实现的,借助动画理解应该很容易。
|
||||
|
||||
「反转以 `a` 为头结点的链表」其实就是「反转 `a` 到 null 之间的结点」,那么如果让你「反转 `a` 到 `b` 之间的结点」,你会不会?
|
||||
|
||||
只要更改函数签名,并把上面的代码中 `null` 改成 `b` 即可:
|
||||
|
||||
```java
|
||||
/** 反转区间 [a, b) 的元素,注意是左闭右开 */
|
||||
ListNode reverse(ListNode a, ListNode b) {
|
||||
ListNode pre, cur, nxt;
|
||||
pre = null; cur = a; nxt = a;
|
||||
// while 终止的条件改一下就行了
|
||||
while (cur != b) {
|
||||
nxt = cur.next;
|
||||
cur.next = pre;
|
||||
pre = cur;
|
||||
cur = nxt;
|
||||
}
|
||||
// 返回反转后的头结点
|
||||
return pre;
|
||||
}
|
||||
```
|
||||
|
||||
现在我们迭代实现了反转部分链表的功能,接下来就按照之前的逻辑编写 `reverseKGroup` 函数即可:
|
||||
|
||||
```java
|
||||
ListNode reverseKGroup(ListNode head, int k) {
|
||||
if (head == null) return null;
|
||||
// 区间 [a, b) 包含 k 个待反转元素
|
||||
ListNode a, b;
|
||||
a = b = head;
|
||||
for (int i = 0; i < k; i++) {
|
||||
// 不足 k 个,不需要反转,base case
|
||||
if (b == null) return head;
|
||||
b = b.next;
|
||||
}
|
||||
// 反转前 k 个元素
|
||||
ListNode newHead = reverse(a, b);
|
||||
// 递归反转后续链表并连接起来
|
||||
a.next = reverseKGroup(b, k);
|
||||
return newHead;
|
||||
}
|
||||
```
|
||||
|
||||
解释一下 `for` 循环之后的几句代码,注意 `reverse` 函数是反转区间 `[a, b)`,所以情形是这样的:
|
||||
|
||||

|
||||
|
||||
递归部分就不展开了,整个函数递归完成之后就是这个结果,完全符合题意:
|
||||
|
||||

|
||||
|
||||
### 三、最后说两句
|
||||
|
||||
从阅读量上看,基本数据结构相关的算法文章看的人都不多,我想说这是要吃亏的。
|
||||
|
||||
大家喜欢看动态规划相关的问题,可能因为面试很常见,但就我个人理解,很多算法思想都是源于数据结构的。我们公众号的成名之作之一,「学习数据结构的框架思维」就提过,什么动规、回溯、分治算法,其实都是树的遍历,树这种结构它不就是个多叉链表吗?你能处理基本数据结构的问题,解决一般的算法问题应该也不会太费事。
|
||||
|
||||
那么如何分解问题、发现递归性质呢?这个只能多练习,也许后续可以专门写一篇文章来探讨一下,本文就到此为止吧,希望对大家有帮助!
|
||||
|
||||
坚持原创高质量文章,致力于把算法问题讲清楚,欢迎关注我的公众号 labuladong 获取最新文章:
|
||||
|
||||

|
146
interview/reverse-nodes-in-k-group.md
Normal file
146
interview/reverse-nodes-in-k-group.md
Normal file
@ -0,0 +1,146 @@
|
||||
# How to reverse nodes in k-group
|
||||
|
||||
**Translator: [Justin](https://github.com/Justin-YGG)**
|
||||
|
||||
**Author: [labuladong](https://github.com/labuladong)**
|
||||
|
||||
We talked about the way how to reverse the part of linked list recursively in [previous article](..算法思维系列/学习数据结构和算法的框架思维.md). Some readers may wonder how to reverse the whole linked list. We also need to use the function of linked list reversion in this article, so we might as well use the recursive method to solve it.
|
||||
|
||||
The problem we need to solve is [Reverse Nodes in k-Group](https://leetcode.com/problems/reverse-nodes-in-k-group/). It's easy to understand what that means.
|
||||
|
||||
> 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`
|
||||
|
||||
We may often encounter this problem in interview and its difficulty is **Hard** on LeetCode. But is it really so tough?
|
||||
|
||||
Actually, the problems of basic data structure are not difficult. We can solve them by splitting the big problem into the small one step by step. I will show you how to do that below.
|
||||
|
||||
### Analysis
|
||||
|
||||
As mentioned in the previous article [the thinking framework of learning data structure](../算法思维系列/学习数据结构和算法的框架思维.md), linked list is a kind of data structure with recursion and iteration. On second thought, we can find that this problem can be solved by recursion.
|
||||
|
||||
What does recursion mean? We can try to understand it with the help of the example below.
|
||||
|
||||
We call `reverseKGroup(head, 2)` on the linked list so that we can reverse the linked list with 2 nodes as a group.
|
||||
|
||||

|
||||
|
||||
What should we do next to deal with the remaining nodes after reversing the first two nodes?The remaining nodes also form a linked list but it's shorter than origin linked list.It turns out to be a subproblem of primal problem.
|
||||
|
||||

|
||||
|
||||
We can call `reverseKGroup(cur, 2)` recursively because there is
|
||||
the same structure between primal problem and subproblem. So, this is so called recursion.
|
||||
|
||||
We can find out the basic procedure of algorithm to solve the problem after understand recursion.
|
||||
|
||||
**1.Reverse the first k nodes**
|
||||
|
||||

|
||||
|
||||
**2. Reverse list with k+1 node as head by calling reverseKGroup recursively**
|
||||
|
||||

|
||||
|
||||
**3. Merge the results of above two steps**
|
||||
|
||||

|
||||
|
||||
Note, there usually is a base case in recursion function. The base case of this problem is **If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is**. I will emphasize it in code.
|
||||
|
||||
### Coding
|
||||
|
||||
First, we need to implement a `reverse` function to reverse the elements in a interval. Before that, let's simplify the problem and consider that how to reverse the linked list with a given head node.
|
||||
|
||||
```java
|
||||
// reverse the linked list with node a as head
|
||||
ListNode reverse(ListNode a) {
|
||||
ListNode pre, cur, nxt;
|
||||
pre = null; cur = a; nxt = a;
|
||||
while (cur != null) {
|
||||
nxt = cur.next;
|
||||
// reverse node one by one
|
||||
cur.next = pre;
|
||||
// update pointer
|
||||
pre = cur;
|
||||
cur = nxt;
|
||||
}
|
||||
// return head node of the reversed linked list
|
||||
return pre;
|
||||
}
|
||||
```
|
||||
|
||||

|
||||
|
||||
It's easy to understand the iteration with the help of animation above.
|
||||
|
||||
When we reverse the linked list with node `a` as head, indeed, we reverse nodes between node `a` and `null`.
|
||||
|
||||
How should we do to reverse nodes between node `a` and `b`?
|
||||
|
||||
Just change the function signature and change `null` to` b` in the above code
|
||||
|
||||
```java
|
||||
/** reverse the nodes of interval [a, b), which is left-closed and right-open */
|
||||
ListNode reverse(ListNode a, ListNode b) {
|
||||
ListNode pre, cur, nxt;
|
||||
pre = null; cur = a; nxt = a;
|
||||
// just change the condition of quit
|
||||
while (cur != b) {
|
||||
nxt = cur.next;
|
||||
cur.next = pre;
|
||||
pre = cur;
|
||||
cur = nxt;
|
||||
}
|
||||
// return head node of the reversed linked list
|
||||
return pre;
|
||||
}
|
||||
```
|
||||
|
||||
So far, we have finished the function of reversing the part of the linked list. Next, we will work on the function of `reverseKGroup` according to the previous design.
|
||||
|
||||
```java
|
||||
ListNode reverseKGroup(ListNode head, int k) {
|
||||
if (head == null) return null;
|
||||
// interval [a, b) includes k nodes to be reversed
|
||||
ListNode a, b;
|
||||
a = b = head;
|
||||
for (int i = 0; i < k; i++) {
|
||||
// base case
|
||||
if (b == null) return head;
|
||||
b = b.next;
|
||||
}
|
||||
// reverse first k nodes
|
||||
ListNode newHead = reverse(a, b);
|
||||
// merge all reversed internals
|
||||
a.next = reverseKGroup(b, k);
|
||||
return newHead;
|
||||
}
|
||||
```
|
||||
|
||||
Note that the interval of `reverse` function is `[a, b)`.
|
||||
|
||||

|
||||
|
||||
We will not give more details about the recursive part again. The result fully meets the meaning of the question:
|
||||
|
||||

|
||||
|
||||
### More
|
||||
|
||||
Only a few people read the algorithm articles related to basic data structure according to the page view. Most of people tend to read the articles related to dynamic programming because they often show up in interview. But what I want to share is basic data structure and algorithm matter a lot and all complicated problems evolve from simple problems.
|
||||
|
||||
By the way, remember that **practice makes perfect**.
|
||||
|
||||
[previous:how to find the longest palindromicsubstring](../interview/The Longest Palindromic Substring.md.md)
|
||||
|
||||
[next:how to valid parentheses](../interview/合法括号判定.md)
|
||||
|
||||
[catalog](../README.md#目录)
|
Reference in New Issue
Block a user