61. Rotate List
来源:互联网 发布:java overload 编辑:程序博客网 时间:2024/06/08 02:24
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
.
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { public ListNode rotateRight(ListNode head, int k) { if (head == null) { return head; } ListNode slow = head, fast = head; int len = 0; while (fast != null) { fast = fast.next; len ++; } int step = k % len; if (step == 0) { return head; } fast = head; while (step != 0) { fast = fast.next; step --; } while (fast.next != null) { fast = fast.next; slow = slow.next; } ListNode currhead = slow.next; slow.next = null; fast.next = head; return currhead; }}
0 0
- [LeetCode]61.Rotate List
- 61. Rotate List
- LeetCode --- 61. Rotate List
- [Leetcode] 61. Rotate List
- 61. Rotate List
- [leetcode] 61.Rotate List
- [leetcode] 61. Rotate List
- 61. Rotate List
- leetcode 61. Rotate List
- LeetCode 61. Rotate List
- [leetcode] 61. Rotate List
- 61. Rotate List
- 61. Rotate List
- 61. Rotate List LeetCode
- leetcode 61. Rotate List
- 61. Rotate List
- 61. Rotate List
- 61. Rotate List
- 05字符串格式化
- 单例模式学习笔记
- 面试题:随机数生成、蓄水池抽样、海量数据、设计秒杀系统
- webpack2.x基础属性讲解(一)
- "尚学堂杯"哈尔滨理工大学第七届程序设计竞赛 G.Great Atm(二进制,位运算)
- 61. Rotate List
- linux/android进程的模块信息获取
- servlet的运行过程
- java NIO详解
- Jsp中JavaBean的用法
- 1. 了解Spring Data JPA
- php安装过程出现的问题
- [BZOJ3697]采药人的路径(点分治)
- MyBatis Generator