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
}
相关推荐
Dream it possible!9 小时前
LeetCode 面试经典 150_多维动态规划_交错字符串(97_C++_中等)
算法·leetcode·面试·动态规划
心抵鹊10 小时前
算法之双指针系列(一)
算法·leetcode
Pniubi12 小时前
力扣560.和为k的子数组
算法·leetcode·职场和发展
Pniubi13 小时前
力扣438.找到字符串中所有字母异位词
算法·leetcode·职场和发展
联犀13 小时前
纯 Go 的 CAD 开源库 go-cad:直接解析 DWG/DXF,现在也能导出 PDF
开发语言·golang·pdf
FungLeo17 小时前
成为全栈·Go 后端篇·三轮代码审阅之后:把可读性和证据做成工程规则
golang·go·ast·静态检查·成为全栈·代码审阅
文丑颜不良啊17 小时前
LeetCode2081~2100题目解析
java·leetcode
文丑颜不良啊19 小时前
LeetCode2061~2080题目解析
java·leetcode
联犀20 小时前
联犀 Go SIMD 实战:视频转码从 16.5ms 到 5.46ms
开发语言·golang·音视频
文丑颜不良啊1 天前
LeetCode1941~1960题目解析
java·leetcode