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;
}
相关推荐
沐晓时光2 小时前
C语言入门,内存函数知识讲解
c语言·开发语言
文丑颜不良啊4 小时前
LeetCode2481~2500题目解析
java·leetcode
Zwawa5 小时前
C语言函数指针与宏实战:从回调、表驱动菜单到X-Macro,讲透嵌入式代码解耦
c语言·单片机·嵌入式
David猪大卫8 小时前
【C++修炼】map 和 set 的使用
c语言·开发语言·数据结构·c++·经验分享·笔记·学习
HPtiger8 小时前
C++ 入门笔记:从 C 到 C++ 的核心过渡(命名空间、输入输出、缺省参数、重载、引用、内联)
c语言·c++·学习
文丑颜不良啊11 小时前
LeetCode2461~2480题目解析
java·leetcode
安妮细水长流12 小时前
ad7124
c语言·单片机
文丑颜不良啊12 小时前
LeetCode2441~2460题目解析
java·leetcode
文丑颜不良啊13 小时前
LeetCode2521~2540题目解析
java·leetcode
文丑颜不良啊16 小时前
LeetCode2501~2520题目解析
java·leetcode