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;
}
相关推荐
Navigator_Z41 分钟前
LeetCode //C - 1289. Minimum Falling Path Sum II
c语言·算法·leetcode
himobrinehacken1 小时前
取模运算的汇编奥秘揭秘
c语言·安全
mmmmath_31 小时前
LeetCode.104.二叉树的最大深度
数据结构·leetcode
青岚_Nox4 小时前
C语言实战:带你手写一个带"计时功能"的猜数字小游戏
c语言
Tisfy6 小时前
LeetCode 2333.最小差值平方和:排序 + 数学计算(不用二分)
数学·算法·leetcode·题解·贪心·排序
aqiu1111116 小时前
【C++算法打怪专栏】LeetCode 217. 存在重复元素(解法总结)
c++·算法·leetcode
iotxiaohu6 小时前
用 cflow 查看 C 函数调用关系
c语言·开发语言·cflow
yi0117 小时前
DAY28: LeetCode 111. 二叉树的最小深度
人工智能·笔记·python·算法·leetcode·二叉树·递归
局部有解8 小时前
Leetcode Hot100 - 最长连续序列
算法·leetcode·职场和发展
iCxhust8 小时前
51单片机电力载波应用开发(九)51产品化
c语言·数据库·嵌入式硬件·mysql·51单片机·软件工程·c51