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;
}
相关推荐
203号居民11 分钟前
LeetCode hot 100 —41. 缺失的第一个正数
数据结构·算法·leetcode
2401_862880824 小时前
数据结构 --- 栈
c语言·数据结构·算法
Tisfy5 小时前
LeetCode 3471.找出最大的几近缺失整数:三种情况判断
算法·leetcode·题解·分类讨论
额额额对了6 小时前
数据结构:二叉树
c语言·数据结构·算法
青 春 记 忆8 小时前
LeetCode 121. 买卖股票的最佳时机|Python 解法详解
python·算法·leetcode
rannn_1119 小时前
【力扣hot100】二叉树专题
算法·leetcode·职场和发展
Navigator_Z10 小时前
LeetCode //C - 1203. Sort Items by Groups Respecting Dependencies
c语言·算法·leetcode
wp123_120 小时前
硬件元器件笔记|IPX8 防水 Type‑C 母座安费诺 124018802112A 与 TONEVEE TY48086‑24A 分析
c语言·开发语言·笔记
luj_176821 小时前
桥牌思维启示:系统设计的模块化架构
c语言·开发语言·c++·经验分享·算法
caimouse1 天前
ReactOS 图形系统分析(16):字符串对象 — STROBJ(string.c)
c语言·开发语言