[Lintcode] #170 旋转链表
来源:互联网 发布:mmd建模软件 编辑:程序博客网 时间:2024/06/16 00:37
给定一个链表,旋转链表,使得每个节点向右移动k个位置,其中k是一个非负数
样例
给出链表1->2->3->4->5->null和k=2
返回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 { /* * @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;int length = 1;ListNode temp = head;while (temp.next != null) {length++;temp = temp.next;}k %= length;if (k == 0)return head;ListNode fast = head;ListNode slow = head;while (k-- > 0) {fast = fast.next;}while (fast.next != null) {slow = slow.next;fast = fast.next;}ListNode re = slow.next;slow.next = null;temp.next = head;return re; }}
阅读全文
0 0
- lintcode-旋转链表-170
- LintCode 170-旋转链表
- lintcode(170)旋转链表
- [Lintcode] #170 旋转链表
- LintCode-旋转链表
- LintCode-旋转链表
- LintCode:旋转链表
- LintCode : 旋转链表
- 旋转链表-LintCode
- [Lintcode]Rotate List旋转链表
- LintCode-旋转图像
- lintcode-旋转图像-161
- LintCode:旋转图像
- LintCode:旋转字符串
- lintcode ----旋转字符串
- LintCode:旋转图像
- Lintcode 恢复旋转数组
- LintCode 8 旋转字符串
- PHP中的ob系列函数
- 指针
- 数据岗位现状调查报告
- MySQL数据库索引的4大类型以及相关的索引创建
- Windows将tomcat配置到开机启动
- [Lintcode] #170 旋转链表
- Python3网络爬虫_使用User Agent和代理IP隐藏身份
- 电路——TTL电平、CMOS电平区别
- 用C为8086写程序(我实际运行了一下, 靠谱! 确实可以用C语言给8086喂指令)
- 习题四(1)
- 开始为开源社区贡献代码了,测试了一把,方法如下
- jsp页面加载静态资源404问题
- node.js实现本地http请求
- centos7 xrdp远程桌面连接