Leetcode Golang 61. Rotate List.go
思路
先把list变成一个环,然后转到指定位置
注意有个取模的处理
code
func rotateRight(head *ListNode, k int) *ListNode {if head == nil || head.Next == nil || k == 0 {return head}len := 1tail := headfor tail.Next != nil {tail = tail.Nextlen++}tail.Next = head //变成一个环k = k % len //因为是个环,所以不用转超过一圈for i := 0; i < len-k; i++ {tail = tail.Next}head = tail.Nexttail.Next = nilreturn head
}
更多内容请移步我的repo:https://github.com/anakin/golang-leetcode
Leetcode Golang 61. Rotate List.go相关推荐
- leetcode 61 Rotate List ----- java
Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given ...
- LeetCode 61. Rotate List
题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example: Gi ...
- leetcode 61. Rotate List
Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL. 定义两个指 ...
- 61. Rotate List
题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example: Gi ...
- 【LeetCode】48. Rotate Image (2 solutions)
Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...
- LeetCode Golang 9.回文数
9. 回文数 第一种办法 :itoa 转换为字符串进行处理: package mainimport ("strconv""fmt" )//判断一个整数是否是回文 ...
- Leetcode Golang 77. Combinations.go
思路 看到这个词,马上就想到了回溯 code func combine(n int, k int) [][]int {var res [][]inthelper(&res, []int{}, ...
- 【LeetCode】61.旋转链表
给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: 1->2->3->4->5->NULL, k = 2 输出: 4-& ...
- 【Golang Leetcode】总目录(Day1~100)
Leetcode Golang Day1~10 Golang每日一练(leetDay0001) 1. 两数之和 Two Sum 2. 两数相加 Add Two Numbers 3. 无重复字符的最长子 ...
最新文章
- 360浏览器5兼容模式吗_Vue-cli项目,打包生产模式,部分用户360浏览器极速模式下奔溃...
- hdu2100 26进制加法
- Opencv判断是否加载图片的两种方法
- 最长连续序列Python解法
- 提取地图中道路_非机器学习方法·从遥感影像中提取道路
- # javascript 总结
- 英特尔固态硬盘540s开卡_英特尔Z490主板绝配?技嘉PCIe 3.0 x8固态硬盘测试:6.5GB/s读速...
- find_first_of和find函数的区别
- 第一章:Python的基本数据类型-第二节:Python中表示“有序”(序列)的数据类型
- boost.asio异步并发Tcp服务器
- 【LeetCode】Combination Sum(组合总和)
- 【转】修改UCGUI源代码以支持多个独立窗体的说明
- 富文本编辑器粘贴图片
- 游程编码run length code
- PHP 获取微视无水印源地址_最新抖音短视频无水印地址解析原理(抖音免费解析接口),以PHP代码为例...
- 一阶微分方程解的存在定理
- Flask HTML模板引擎详解
- 【PHP】如何提高网页加载速度?
- RFSOC 6U VPX标准板卡
- 基础乐理 - 反复记号