LintCode:旋转链表
2015-11-24 23:03
260 查看
给定一个链表,旋转链表,使得每个节点向右移动k个位置,其中k是一个非负数
您在真实的面试中是否遇到过这个题?
Yes
样例
给出链表1->2->3->4->5->null和k=2
返回4->5->1->2->3->null
标签 Expand
相关题目 Expand
您在真实的面试中是否遇到过这个题?
Yes
样例
给出链表1->2->3->4->5->null和k=2
返回4->5->1->2->3->null
标签 Expand
相关题目 Expand
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { /** * @param head: the List * @param k: rotate to the right k places * @return: the list after rotation */ public ListNode rotateRight(ListNode head, int k) { // write your code here if(head == null ) return null; ListNode c = head; int count = 0; while(c!=null){ count++; c = c.next; } ListNode x = head; ListNode res = new ListNode(-1); ListNode y = res; ListNode front = new ListNode(-1); ListNode z = front; for(int i=0;i<count-k%count&&x!=null;i++){ z.next = x; z = z.next; x = x.next; } z.next = null; while(x!=null){ y.next = x; y = y.next; x = x.next; } y.next = front.next; return res.next; } }
相关文章推荐
- 关于addSpriteFramesWithFile读取失败
- 求两个并联电阻的值
- 如何在datetime.strptime中添加时区
- qtcreator minggw 支持c++11
- finereport的服务器负载均衡部署--tomcat
- http://macrochen.iteye.com/blog/1414568
- LintCode:旋转图像
- 开源的DevOps开发工具箱
- Matlab 实现矩阵的满秩分解(最大秩分解)
- 错误处理
- 第二百三十六天 how can I 坚持
- odoo worker 异常Exception("bus.Bus unavailable")
- Java FP: Java中函数式编程的Map和Fold(Reduce)
- 对数据库中完整性概述的认识
- Coderforce #332 D. Spongebob and Squares
- 窗体运行前,检查是否互斥
- 原生JS练习计算器和banner条滚动效果
- lua学习03——table
- IOS开发真机调试
- 静态路由配置实例