[leetcode]61. Rotate List@Java解题报告
来源:互联网 发布:折八百淘宝商城马甲 编辑:程序博客网 时间:2024/06/05 02:32
https://leetcode.com/problems/rotate-list/description/
Given a list, rotate the list to the right by k places, where k is non-negative.
For example:
Given 1->2->3->4->5->NULL
and k = 2
,
return 4->5->1->2->3->NULL
.
这道题主要先理解题意,就是倒着数k个node,从那开始到结尾和之前那部分对调,那个例子就是,4->5拿前面来,1->2->3拿后面去。
package go.jacob.day817;/** * 61. Rotate List * @author Jacob */public class Demo2 {/* * 这种方法考虑了k=0的情况。 * 当k=0,cur和pre都为链表最后一个节点 * 运行cur.next = preHead.next;preHead.next = pre.next;pre.next = null;后 * 链表结构没有改变 */public ListNode rotateRight(ListNode head, int k) {if (k == 0 || head == null || head.next == null)return head;ListNode preHead = new ListNode(0);preHead.next = head;ListNode cur = head;ListNode pre = head;int total;for (total = 1; cur.next != null; total++)cur = cur.next;for (int i = 1; i < total - k % total; i++) {pre = pre.next;}cur.next = preHead.next;preHead.next = pre.next;pre.next = null;return preHead.next;}}
阅读全文
0 0
- [leetcode]61. Rotate List@Java解题报告
- [leetcode] 61. Rotate List 解题报告
- [Leetcode] 61. Rotate List 解题报告
- 【LeetCode】Rotate List 解题报告
- [LeetCode] Rotate List 解题报告
- Leetcode #61. Rotate List 循环位移链表 解题报告
- LeetCode-Rotate Array-解题报告
- [LeetCode] Rotate Image 解题报告
- (Java)LeetCode-61. Rotate List
- 【leetcode】61. Rotate List【java】
- [LeetCode] 61. Rotate List java
- [leetcode]86. Partition List@Java解题报告
- leetcode解题61. Rotate List java版(旋转链表)
- [Leetcode] Rotate List (Java)
- [LeetCode][Java] Rotate List
- leetcode:Rotate List 【Java】
- Rotate List leetcode java
- [leetcode] 48. Rotate Image 解题报告
- tomcat启动时报:IOException while loading persisted sessions: java.io.EOFException的解决方案
- A + B Problem II(大整数加法)
- apache和tomcat
- Flume安装及简单使用
- 数据组件
- [leetcode]61. Rotate List@Java解题报告
- 深度学习TensorFlow如何使用多GPU并行模式?
- 定时器和多线程
- 正则表达式Regular Expression
- 累了,有点迷茫了
- c# 限制TEXTBOX输入,允许输入整数,小数等
- springmvc 上传文件到服务器
- SSL2703 2017年8月16日提高组T2 疾病(dfs)
- CentOS下快速安装配置OpenVPN的方法(3分钟内)