Leetcode:Rotate List 链表旋转

Rotate List

Given a list, rotate the list to the right by k places,
where k is non-negative.

For
example:
Given 1->2->3->4->5->NULL and k = 2,
return 4->5->1->2->3->NULL.

解题分析:

不同于数组旋转,数组可以随机存取,数组的旋转可以巧妙的 分成两两部分递归旋转

对于链表的旋转,实际上就是把后一部分连续的尾插法插入到新的结果链表中


class Solution {
public:
ListNode *rotateRight(ListNode *head, int k) {
assert(k >= 0);
if (head == nullptr) return nullptr;
ListNode* cur = head;
int listLen = 0;
while (cur != NULL) {
++listLen;
cur = cur->next;
}
k %= listLen;

ListNode* newHead = new ListNode(0);
newHead->next = head;

ListNode* resHead = new ListNode(0);
ListNode* resCur = resHead;

cur = newHead;
for (int i = 0; i < listLen - k; ++i) {
cur = cur->next;
}

ListNode* resTail = cur;
for (int i = 0; i < k; ++i) {
ListNode* nextNode = cur->next;
ListNode* tmp = nextNode->next;
resCur->next = nextNode;
resCur = nextNode;
cur->next = tmp;
}
resCur->next = newHead->next;
resTail->next = NULL;
return resHead->next;
}
};

需要注意以下几点:

1. K的取值,因为k值可以很大,我们可以将k对链表长度取余,因为每旋转一个链表长度相当于复原了

2. 虚拟前置结点指向链表的第一个结点,因为头节点也可能被修改,而头节点的修改操作和其余结点的操作不统一,因此采用一个虚拟的前置结点

3. 我们从虚拟的前置结点newHead前进listLen - k步达到需要移动区间的前一个节点,因为每删除一个结点需要知道其前驱指针

4.
循环之后,将原链表剩下的结点一起尾插到结果链表中,并且需要将结果链表最后一个结点的next置为NULL,即使用暂存结点 resTail->next
= NULL;

5. 返回的头节点应该是虚拟的前置结点的next指针

Leetcode:Rotate List 链表旋转,布布扣,bubuko.com

时间: 2024-08-04 10:11:56

Leetcode:Rotate List 链表旋转的相关文章

[LeetCode] Rotate Image [26]

题目 You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up: Could you do this in-place? 原题链接(点我) 解题思路 顺时针方向旋转数组90°.这个题也是个没啥意思的题,自己画画图,找找规律.就出来了.我举一个n=4的例子还说明下规律: 通过图可以看出A[0][0] = A[3][0],.....从这些中

LeetCode: Rotate List [060]

[题目] Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL. [题意] 给定一个链表L,和非负数K,要求把链表的后k个节点移到链表前 [思路] 先将指针指向指向倒数第K个节点,然后反转

LeetCode: Rotate Image [047]

[题目] You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up: Could you do this in-place? [题意] 给定一个nXn的二维矩阵,按时钟方向旋转90度,不能使用额外的数据结构 [思路] 从外向内逐层旋转 [代码] class Solution { public: void rotateMatrix(vec

Leetcode:Partition List 链表快速排序划分

Partition List Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. For example,Given

Linked List Cycle leetcode java (链表检测环)

题目: Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? 题解: 这道题连带着II是很经典的,在看CC150时候,纠结这个问题纠结了很久.在读了很多网上资料还有书的讲解以及和别人讨论之后,对这个专题终于明白了. 这一问只需要判断链表是否有环. 当链表没有环时是很好判断的,让一个指针一直往后走,遇见null了自然就没有环. 而如

图解精选 TOP 面试题 001 | LeetCode 237. 删除链表中的节点

题目描述 原题链接 LeetCode 237. 删除链表中的节点:https://leetcode-cn.com/problems/delete-node-in-a-linked-list 请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点. 现有一个链表 head =?[4,5,1,9],它可以表示为: 示例 1: 输入: head = [4,5,1,9], node = 5 输出: [4,1,9] 解释: 给定你链表中值为 ?5? 的第二个节点,那么在调

translate移动坐标,rotate实现2D自动旋转,translate和rotate实现2D旋转

1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 6 <title>translate移动坐标,rotate实现2D自动

[LeetCode] Rotate List 旋转链表

Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL. 这道旋转链表的题和之前那道Rotate Array 旋转数组 很类似,但是比那道要难一些,因为链表的值不能通过下表来访问,只能一个一个的

[LeetCode] Rotate List 单项链表旋转

Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL. Hide Tags Linked List Two Pointers 这题有点难理解,k 的意思是链表右起第k 个,k 大于链的个数时候