Golang | Leetcode Golang题解之第61题旋转链表

题目:

题解:

Go 复制代码
func rotateRight(head *ListNode, k int) *ListNode {
    if k == 0 || head == nil || head.Next == nil {
        return head
    }
    n := 1
    iter := head
    for iter.Next != nil {
        iter = iter.Next
        n++
    }
    add := n - k%n
    if add == n {
        return head
    }
    iter.Next = head
    for add > 0 {
        iter = iter.Next
        add--
    }
    ret := iter.Next
    iter.Next = nil
    return ret
}
相关推荐
Pniubi8 小时前
力扣283移动零(双指针法)
算法·leetcode·职场和发展
旖旎夜光18 小时前
LeetCode 315: 算右侧小于当前元素的个数(归并排序) —— 题解
c++·学习·算法·leetcode·排序算法
文丑颜不良啊19 小时前
LeetCode1401~1420题目解析
java·leetcode
Nuanyt19 小时前
LeetCode Hot 100 链表专题2 3 题 (Java)
java·笔记·算法·leetcode·链表
文丑颜不良啊19 小时前
LeetCode1341~1360题目解析
java·leetcode
紧跟先前的步伐19 小时前
Go AI Agent 实战 22|完善 Agent 执行循环:从工具调用到最终回答
人工智能·ai·golang
海绵宝宝转agent19 小时前
LeetCodehot100题解
java·笔记·学习·算法·leetcode·深度优先
后台模板学习21 小时前
Go Web开发中任务调度框架的源码解析与高可用实现方案
开发语言·后端·golang
FungLeo21 小时前
成为全栈·Go 后端篇·分类树与标签关系:目录变更为何需要协调
golang·go·gorm·数据库事务·分类树·成为全栈
Pniubi21 小时前
力扣543二叉树的直径(深度优先搜索法(递归))
算法·leetcode·职场和发展