LeetCode Solutions : Rotate List
来源:互联网 发布:百旺金赋税控软件 编辑:程序博客网 时间:2024/05/11 04:49
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; * next = null; * } * } */public class Solution { public ListNode rotateRight(ListNode head, int n) {if(head==null||head.next==null)return head; ListNode rear=head;int len=1;while(rear.next!=null){rear=rear.next;len++;}rear.next=head;int breakPoint=len-n%len;rear=head;int count=1;while(count<breakPoint){rear=rear.next;count++;}head=rear.next;rear.next=null;return head; }}
3 0
- LeetCode Solutions : Rotate List
- LeetCode Solutions : Rotate List
- LeetCode Solutions : Reorder List
- LeetCode Solutions : Sort List
- [leetcode][list] Rotate List
- LeetCode: Rotate List
- LeetCode Rotate List
- LeetCode : Rotate List
- [Leetcode] Rotate List
- [LeetCode] Rotate List
- [leetcode] Rotate List
- leetcode 70: Rotate List
- [Leetcode] Rotate List
- 【leetcode】Rotate List
- [LeetCode]Rotate List
- [leetcode]Rotate List
- LeetCode-Rotate List
- [leetcode] Rotate List
- 谱聚类算法(Spectral Clustering)
- STM32与PID算法
- zoj3686(重新编号的线段树)
- 麻省理工学院公开课:算法导论1
- 树莓派学习笔记——apt方式安装opencv
- LeetCode Solutions : Rotate List
- Android Zip解压的问题
- GitHub 优秀的 Android 开源项目(值得研究)
- 设计模式-工厂模式
- cocos2dx 2.0-3.0 过渡篇
- 字符和字符串的输入输出(经典)
- lvs安装与配置
- 这就是我
- 双色球小程序 从1~33个数中抽出不重复的6个数为红球,然后从1~16中抽出1个数是蓝球