LeetCode:Rotate List
来源:互联网 发布:淘宝卖家电话 编辑:程序博客网 时间:2024/06/05 14:47
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
.
首先计算链表长度,将链表首尾相接,之后得出链表从head开始起的正向偏移量,从head开始遍历找到旋转后的head,再“扯断”成环的链表,返回新的head.
/** * 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) { ListNode p=head; if(head==null || n==0)return head; int len=1; while(p.next!=null) { p=p.next; len++; } p.next=head; n=len-n%len; while(n>0) { p=p.next; n--; } head=p.next; p.next=null; return head; }}
0 0
- [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
- LeetCode - Rotate List
- LeetCode:Rotate List
- LeetCode 61: Rotate List
- leetcode之Rotate List
- 黑马程序员 基础测试题
- GitHub could not read Username
- 如何使用strace+pstack利器分析程序性能
- Oracle sysdate 时间加减
- hibernate4 和 spring3 整合注意事项 否则java.lang.NoSuchMethodError异常
- LeetCode:Rotate List
- A*算法
- 监控系统调用工具strace
- LINUX下USB1.1设备学习小记(4)_uhci(3)
- 线程安全类的设计
- java中两个字符串如何比较大小
- NoteBook Daily
- SQL语句--按类型每3组数据取一次总和
- 动态样式语言Sass&Less