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;
}
相关推荐
铅笔小新z2 小时前
【C语言】操作符
c语言
by209994 小时前
从 malloc/free 到 new/delete:真正理解 C++ 中的内存管理与对象生命周期
c语言·c++·经验分享·动态内存管理
额额额对了4 小时前
SPI通信
linux·c语言·汇编·单片机·嵌入式硬件·arm
王老师青少年编程4 小时前
2026年9月GESP真题及题解(C++七级):括号序列
题解·真题·gesp·csp·c++七级·括号序列·2026年9月
Logic1015 小时前
C语言/数据结构位运算题解:异或XOR找出书柜中编号只出现一次的那一层——只出现一次的数字
c语言·数据结构·数组·位运算·时间复杂度·算法题·异或性质
smj2302_796826525 小时前
解决leetcode第4064题至多一次取反能被k整除的最长子数组II
python·算法·leetcode
青山木5 小时前
Hot 100 --- 下一个排列
java·数据结构·算法·leetcode
垆边人似月.5 小时前
字符串重排后最大字典序
算法·leetcode·职场和发展
代码什么用6 小时前
LeetCode-42. 接雨水
算法·leetcode
Logic1017 小时前
C语言/数据结构位运算题解:异或XOR找出星球探险中的“独特能量石“——只出现一次的数字
c语言·数据结构·数组·位运算·时间复杂度·算法题·异或性质