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
}
相关推荐
AlenTech27 分钟前
139. 单词拆分 - 力扣(LeetCode)
算法·leetcode·职场和发展
dovens43 分钟前
GO 快速升级Go版本
开发语言·redis·golang
Lufeidata1 小时前
go语言学习记录-入门阶段
前端·学习·golang
穿条秋裤到处跑2 小时前
每日一道leetcode(2026.03.30):判断通过操作能否让字符串相等 II
算法·leetcode
Q741_1472 小时前
每日一题 力扣 2840. 判断通过操作能否让字符串相等 II 力扣 2839. 判断通过操作能否让字符串相等 I 找规律 字符串 C++ 题解
c++·算法·leetcode·力扣·数组·找规律
我真不是小鱼2 小时前
cpp刷题打卡记录24——路径总和 & 路径总和II
数据结构·c++·算法·leetcode
nianniannnn2 小时前
力扣 347. 前 K 个高频元素
c++·算法·leetcode
x_xbx2 小时前
LeetCode:217. 存在重复元素
数据结构·leetcode·哈希算法
Frostnova丶2 小时前
LeetCode 2839. 判断通过操作能否让字符串相等 I
算法·leetcode
de_wizard3 小时前
Linux 下安装 Golang环境
linux·运维·golang