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
}
相关推荐
做怪小疯子16 小时前
LeetCode 热题 100——二叉树——翻转二叉树
算法·leetcode·职场和发展
谷隐凡二17 小时前
Go语言实现Kubernetes主从架构模拟系统
架构·golang·kubernetes
做怪小疯子17 小时前
LeetCode 热题 100——二叉树——二叉树的最大深度
算法·leetcode·职场和发展
Maỿbe18 小时前
暴打力扣之优先级队列(堆)
算法·leetcode·职场和发展
Swift社区18 小时前
LeetCode 438 - 找到字符串中所有字母异位词
算法·leetcode·职场和发展
学学学无无止境18 小时前
力扣-位1的个数
leetcode
别学LeetCode18 小时前
#leetcode# 1
leetcode
b***653218 小时前
Go-Gin Web 框架完整教程
前端·golang·gin
sheeta199818 小时前
LeetCode 每日一题笔记 日期:2025.11.30 题目:1590.使数组和能被 P 整除
笔记·算法·leetcode
皖南大花猪18 小时前
Go 项目中使用 Casbin 实现 RBAC 权限管理完整教程
开发语言·后端·golang·rbac·casbin