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
}
相关推荐
52Hz1189 小时前
力扣230.二叉搜索树中第k小的元素、199.二叉树的右视图、114.二叉树展开为链表
python·算法·leetcode
苦藤新鸡9 小时前
56.组合总数
数据结构·算法·leetcode
菜鸟233号9 小时前
力扣647 回文子串 java实现
java·数据结构·leetcode·动态规划
LiLiYuan.9 小时前
【Cursor 中找不到LeetCode 插件解决办法】
算法·leetcode·职场和发展
Charlie_lll9 小时前
力扣解题-[3379]转换数组
数据结构·后端·算法·leetcode
TracyCoder12310 小时前
LeetCode Hot100(23/100)——142. 环形链表 II
算法·leetcode·链表
有代理ip10 小时前
Python 与 Golang 爬虫的隐藏优势
爬虫·python·golang
TracyCoder12311 小时前
LeetCode Hot100(28/100)——104. 二叉树的最大深度
算法·leetcode
执着25911 小时前
力扣hot100 - 101、对称二叉树
数据结构·算法·leetcode
天远云服12 小时前
天远车辆过户查询API微服务实战:用Go语言构建高性能车况溯源系统
大数据·微服务·架构·golang