LeetCode //C - 61. Rotate List

这篇具有很好参考价值的文章主要介绍了LeetCode //C - 61. Rotate List。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

61. Rotate List

Given the head of a linked list, rotate the list to the right by k places.
 

Example 1:

LeetCode //C - 61. Rotate List,LeetCode,leetcode,c语言,算法

Input: head = [1,2,3,4,5], k = 2
Output: [4,5,1,2,3]

Example 2:

LeetCode //C - 61. Rotate List,LeetCode,leetcode,c语言,算法

Input: head = [0,1,2], k = 4
Output: [2,0,1]

Constraints:
  • The number of nodes in the list is in the range [0, 500].
  • -100 <= Node.val <= 100
  • 0 < = k < = 2 ∗ 1 0 9 0 <= k <= 2 * 10^9 0<=k<=2109

From: LeetCode
Link: 61. Rotate List
文章来源地址https://www.toymoban.com/news/detail-691991.html


Solution:

Ideas:
  1. Find the Length: First, traverse the list to find its length n.
  2. Calculate Effective Rotation: Since rotating a list of length n places is the same as not rotating it at all, we only need to rotate k mod n places.
  3. Find New Head: Traverse the list to the (n−k mod n)th node. This will be the new tail after rotation.
  4. Perform Rotation: Update the next pointer of the new tail to NULL and set the next pointer of the old tail to the old head.
Code:
struct ListNode* rotateRight(struct ListNode* head, int k) {
    if (head == NULL || k == 0) {
        return head;
    }
    
    // Step 1: Find the length of the list
    int n = 1;
    struct ListNode *tail = head;
    while (tail->next != NULL) {
        n++;
        tail = tail->next;
    }
    
    // Step 2: Calculate the effective number of rotations needed
    k = k % n;
    if (k == 0) {
        return head;
    }
    
    // Step 3: Find the new head and tail
    struct ListNode *new_tail = head;
    for (int i = 0; i < n - k - 1; i++) {
        new_tail = new_tail->next;
    }
    struct ListNode *new_head = new_tail->next;
    
    // Step 4: Perform the rotation
    new_tail->next = NULL;
    tail->next = head;
    
    return new_head;
}

到了这里,关于LeetCode //C - 61. Rotate List的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • LeetCode in Python 48. Rotate Image/Matrix (旋转图像/矩阵)

    旋转图像/矩阵的重点是寻找旋转前后对应位置的坐标关系。 示例: 图1 旋转图像/矩阵的输入输出示意图  代码:  解释: 1)外层循环控制需要转的大圈圈数,内层循环控制每一圈需要转的小圈圈数,大小圈数的解释见图2,例如n=4,需要循环n // 2 = 2大圈,其中黄色循环箭头

    2024年04月27日
    浏览(41)
  • 【LeetCode 算法】Linked List Cycle II 环形链表 II

    给定一个链表的头节点 head ,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。 如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,评测系统内部使用整数 pos 来表示链表尾连接到链表中的位置(索引从

    2024年02月14日
    浏览(32)
  • HOT 100(61~80)【LeetCode】

    2023-7-2 10:25:40 HOT 100(1~20)【LeetCode】 HOT 100(21~40)【LeetCode】 HOT 100(41~60)【LeetCode】 课程表 提示 中等 1.6K 相关企业 你这个学期必须选修 numCourses 门课程,记为 0 到 numCourses - 1 。 在选修某些课程之前需要一些先修课程。 先修课程按数组 prerequisites 给出,其中 prerequisite

    2024年02月12日
    浏览(25)
  • leetcode61. 旋转链表(java)

    Leetcode链接: https://leetcode.cn/problems/rotate-list/ 给你一个链表的头节点 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.va

    2024年02月08日
    浏览(41)
  • 【leetcode】61. 旋转链表 (python)

    题目链接:61.旋转链表

    2024年02月15日
    浏览(28)
  • Leetcode-每日一题【61.旋转链表】

    给你一个链表的头节点  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 1.根据题目给出

    2024年02月11日
    浏览(26)
  • 61. 旋转链表、Leetcode的Python实现

    博客主页:🏆李歘歘的博客 🏆 🌺每天不定期分享一些包括但不限于计算机基础、算法、后端开发相关的知识点,以及职场小菜鸡的生活。🌺 💗点关注不迷路,总有一些📖知识点📖是你想要的💗 ⛽️今天的内容是     Leetcode  61. 旋转链表    ⛽️💻💻💻 61. 旋转链表

    2024年02月06日
    浏览(30)
  • 【Leetcode每日一题】 动态规划 - 地下城游戏(难度⭐⭐⭐)(61)

    1. 题目解析 题目链接:174. 地下城游戏 这个问题的理解其实相当简单,只需看一下示例,基本就能明白其含义了。 2.算法原理 一、状态表定义 在解决地下城游戏问题时,我们首先需要对状态进行恰当的定义。一个直观的想法是,从起点开始,到达[i, j]位置时所需的最低初始

    2024年04月29日
    浏览(35)
  • leetcode - 86. Partition List

    Given the head of 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. Example 1: Example 2: Constraints: Use two list node and two tail node for smaller nodes and bigger nodes. Every time add the node

    2024年02月12日
    浏览(21)
  • LeetCode //C - 86. Partition List

    Given the head of 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.   Example 1: Input: head = [1,4,3,2,5,2], x = 3 Output: [1,2,2,4,3,5] Example 2: Input: head = [2,1], x = 2 Output: [1,2] Const

    2024年02月10日
    浏览(25)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包