当前位置:网站首页>C语言力扣第61题之旋转链表。双端队列与构造循环链表
C语言力扣第61题之旋转链表。双端队列与构造循环链表
2022-07-29 04:12:00 【管二狗绝不摆烂】
给你一个链表的头节点 head ,旋转链表,将链表每个节点向右移动 k 个位置。
示例 1:
输入:head = [1,2,3,4,5], k = 2
输出:[4,5,1,2,3]
示例 2:
输入:head = [0,1,2], k = 4
输出:[2,0,1]
提示:
链表中节点的数目在范围 [0, 500] 内
-100 <= Node.val <= 100
0 <= k <= 2 * 109
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/rotate-list
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
方法一:
记给定链表的长度为 n,注意到当向右移动的次数 k≥nk 时,我们仅需要向右移动 k mod nk 次即可。因为每 nnn 次移动都会让链表变为原状。这样我们可以知道,新链表的最后一个节点为原链表的第 (n−1)−(k mod n)(n - 1)个节点(从 0 开始计数)。
这样,我们可以先将给定的链表连接成环,然后将指定位置断开。
具体代码中,我们首先计算出链表的长度 nnn,并找到该链表的末尾节点,将其与头节点相连。这样就得到了闭合为环的链表。然后我们找到新链表的最后一个节点(即原链表的第 (n−1)−(k mod n)个节点),将当前闭合为环的链表断开,即可得到我们所需要的结果。
特别地,当链表长度不大于 1,或者 k 为 n 的倍数时,新链表将与原链表相同,我们无需进行任何处理。
typedef struct ListNode ListNode;
struct ListNode *rotateRight(struct ListNode *head, int k) {
if (!head) {
return head;
}
int length = 1;
ListNode *tail = head;
ListNode *temp = head;
while (tail->next) {
tail = tail->next;
length++;
}
//把链表尾和链表头相连,循环链表
tail->next = head;
//链表向后移动k步
k = k % length;
for (int i = 0; i < length - k; ++i) {
temp = temp->next;
}
ListNode *res = temp;
for (int i = 0; i < length - 1; ++i) {
temp = temp->next;
}
temp->next = NULL;
return res;
}方法二:
如样例所示,head = [1,2,3,4,5],k = 2,我们输出[4,5,1,2,3]。下面来讲解模拟的做法。
假设链表的长度为n,为了将链表每个节点向右移动 k 个位置,我们只需要将链表的后 k % n个节点移动到链表的最前面,然后将链表的后k % n个节点和前 n - k个节点连接到一块即可。
具体过程如下:
1、首先遍历整个链表,求出链表的长度n,并找出链表的尾节点tail。

2、由于k可能很大,所以我们令 k = k % n,然后再次从头节点head开始遍历,找到第n - k个节点p,那么1 ~ p是链表的前 n - k个节点,p+1 ~ n是链表的后k个节点。

3、接下来就是依次执行 tail->next = head,head = p->next,p->next = nullptr,将链表的后k个节点和前 n - k个节点拼接到一块,并让head指向新的头节点(p->next),新的尾节点即p节点的next指针指向null。

4、最后返回链表的新的头节点head。
struct ListNode* rotateRight(struct ListNode* head, int k) {
struct ListNode* arr[501];
struct ListNode *dummy = head;
struct ListNode *temp = NULL;
int count = 0;
while (dummy != NULL) {
arr[count++] = dummy;
dummy = dummy->next;
}
if (count <= 1) {
return head;
}
k %= count;
arr[count - 1]->next = arr[0];
temp = arr[count - 1 - k]->next;
arr[count - 1 - k]->next = NULL;
return temp;
}
边栏推荐
- BIO、NIO、AIO的区别和原理
- SQL server当存储过程接收的参数是int类型时,如何做判断?
- The data source is SQL server. I want to configure the incremental data of the last two days of the date field updatedate to add
- 2021 sist summer camp experience + record post of School of information, Shanghai University of science and technology
- 数据挖掘——关联分析基础介绍(上)
- Ssl== certificate related concepts
- [kvm] common commands
- AssertionError(“Torch not compiled with CUDA enabled“)
- Install the laser of ROS_ scan_ Problems encountered in match library (I)
- [untitled]
猜你喜欢

Ssl== certificate related concepts
![[paper translation] vectornet: encoding HD maps and agent dynamics from vectorized representation](/img/4b/150689d5e4809ae66a4297915ecd0c.png)
[paper translation] vectornet: encoding HD maps and agent dynamics from vectorized representation

C语言实现三子棋游戏(详解)

Is the array name a pointer

SQL window function

When array is used as a function parameter, it is better to use the array size as a function parameter

【深度学习CPU(番外篇)——虚拟内存】

Locally call tensorboard and Jupiter notebook on the server (using mobaxterm)

AssertionError(“Torch not compiled with CUDA enabled“)

MPU6050
随机推荐
When array is used as a function parameter, it is better to use the array size as a function parameter
[principle] several ways of horizontal penetration
Pat a1069/b1019 the black hole of numbers
After I get the winfrom specific control ID from the database, I need to find the corresponding control through this ID and assign a value to the text text of the control. What should I do
Change the value of the argument by address through malloc and pointer
有没有大佬帮我看下flink sql连接kafka认证kerberos的参数配置是否有误
Copy products with one click from Taobao, tmall, 1688, wechat, jd.com, Suning, taote and other platforms to pinduoduo platform (batch upload baby details Interface tutorial)
店铺排名问题,如何解决?
pat A1041 Be Unique
SVG--loading动画
How to set the SQL execution timeout for flick SQL
"Weilai Cup" 2022 Niuke summer multi school training camp 1 J serval and essay (heuristic merger)
[kvm] install KVM
The difference between dynamic, VaR and object in fluent
Fuzzy query of SQL
C语言实现三子棋游戏(详解)
有一种密码学专用语言叫做ASN.1
LDP --- 标签分发协议
淘宝商品详情接口(商品详情页面数据接口)
There is a special cryptology language called asn.1