C语言 | Leetcode C语言题解之第61题旋转链表

题目:

题解:

cpp 复制代码
struct ListNode* rotateRight(struct ListNode* head, int k) {
    if (k == 0 || head == NULL || head->next == NULL) {
        return head;
    }
    int n = 1;
    struct ListNode* iter = head;
    while (iter->next != NULL) {
        iter = iter->next;
        n++;
    }
    int add = n - k % n;
    if (add == n) {
        return head;
    }
    iter->next = head;
    while (add--) {
        iter = iter->next;
    }
    struct ListNode* ret = iter->next;
    iter->next = NULL;
    return ret;
}
相关推荐
踩坑记录5 分钟前
leetcode hot100 74. 搜索二维矩阵 二分查找 medium
leetcode
TracyCoder1235 分钟前
LeetCode Hot100(60/100)——55. 跳跃游戏
算法·leetcode
Sunsets_Red31 分钟前
P8277 [USACO22OPEN] Up Down Subsequence P 题解
c语言·c++·算法·c#·学习方法·洛谷·信息学竞赛
小刘爱玩单片机42 分钟前
【stm32简单外设篇】- 测速传感器模块(光电)
c语言·stm32·单片机·嵌入式硬件
Charlie_lll44 分钟前
力扣解题-438. 找到字符串中所有字母异位词
后端·算法·leetcode
hateregiste1 小时前
嵌入式软件开发中常见知识点问答集锦!
c语言·单片机·嵌入式软件
极客小张1 小时前
基于STM32的智能水质监测与远程预警系统设计与实现
c语言·python·stm32·单片机·嵌入式硬件·物联网
Once_day1 小时前
GCC编译(7)链接脚本LinkerScripts
c语言·c++·编译和链接·程序员自我修养
小刘爱玩单片机1 小时前
【stm32简单外设篇】- KY-025 干簧管(磁控)模块
c语言·stm32·单片机·嵌入式硬件