Rotate List
来源:互联网 发布:淘宝店一共有多少分 编辑:程序博客网 时间:2024/05/29 10:52
题目
Given a list, rotate the list to the right by k places, where k is non-negative.
For example:
Given1->2->3->4->5->NULL
and k =2
,
return4->5->1->2->3->NULL
.
方法
链表的旋转。public ListNode rotateRight(ListNode head, int n) { if (head == null) { return null; } ListNode node = head; int k = 0; while (node != null) { k++; node = node.next; } int len = k; n = n % len; if (n == 0) { return head; } ListNode start = new ListNode(0); start.next = head; ListNode pre = null; ListNode end = null; int i = 0; end = start; while (end.next != null) { end = end.next; if (pre != null) { pre = pre.next; } i++; if (i == n) { pre = start; } } if (i > n) { end.next = start.next; start.next = pre.next; pre.next = null; } return start.next; }
0 0
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- rotate-list
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- YModem协议
- 关于genymotion启动虚拟机的问题解决
- 赵雅智_ContentProvider
- 【leetcode】sort list
- WeifenLuo.WinFormsUI.Docking的 右键 的个性处理
- Rotate List
- Unsupported compiler 'com.apple.compilers.llvmgcc42' selected for architecture 'armv7'
- fedora系统布的tomcat服务在其他电脑上访问不到
- 第十六周ACM(1)
- 源码安装libvirt 常见问题
- hive--2014.6.10
- mac下安装安卓开发环境
- jQuery 表格排序插件 Tablesorter 使用
- js自己写的一个手机电话同时可以验证的正则表达式