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
}
相关推荐
thusloop3 小时前
380. O(1) 时间插入、删除和获取随机元素
数据结构·算法·leetcode
緈福的街口4 小时前
【leetcode】584. 寻找用户推荐人
算法·leetcode·职场和发展
Maybyy4 小时前
力扣242.有效的字母异位词
java·javascript·leetcode
wjcurry4 小时前
完全和零一背包
数据结构·算法·leetcode
卜锦元5 小时前
Go中使用wire进行统一依赖注入管理
开发语言·后端·golang
mit6.82410 小时前
论容器化 | 分析Go和Rust做医疗的后端服务
docker·golang·rust
ykuaile_h810 小时前
Go 编译报错排查:vendor/golang.org/x/crypto/cryptobyte/asn1 no Go source files
后端·golang
科大饭桶11 小时前
数据结构自学Day5--链表知识总结
数据结构·算法·leetcode·链表·c
YuTaoShao13 小时前
【LeetCode 热题 100】24. 两两交换链表中的节点——(解法一)迭代+哨兵
java·算法·leetcode·链表
前端拿破轮14 小时前
翻转字符串里的单词,难点不是翻转,而是正则表达式?💩💩💩
算法·leetcode·面试