61. Rotate List
来源:互联网 发布:java项目总结 编辑:程序博客网 时间:2024/06/09 16:44
Given a list, rotate the list to the right by k places, where k is non-negative.
Solution 2:
For example:
Given 1->2->3->4->5->NULL
and k = 2
,
return 4->5->1->2->3->NULL
.
Subscribe to see which companies asked this question
My naive solution:
public class Solution { public ListNode rotateRight(ListNode head, int k) { if(head==null||head.next==null) return head; ListNode now=head; int count=0; while(now!=null){ count++; now=now.next; } k%=count; while(k--!=0){ now=head; while(now.next.next!=null) now=now.next; now.next.next=head; head=now.next; now.next=null; } return head; }}
Solution 2:
public class Solution { public ListNode rotateRight(ListNode head, int k) { if(head==null||head.next==null) return head; ListNode temp=head; int count=1; while(head.next!=null){ head=head.next; count++; } head.next=temp; k%=count; head=temp; for(int i=1;i<count-k;++i) head=head.next; temp=head.next; head.next=null; return temp; }}
0 0
- [LeetCode]61.Rotate List
- 61. Rotate List
- LeetCode --- 61. Rotate List
- [Leetcode] 61. Rotate List
- 61. Rotate List
- [leetcode] 61.Rotate List
- [leetcode] 61. Rotate List
- 61. Rotate List
- leetcode 61. Rotate List
- LeetCode 61. Rotate List
- [leetcode] 61. Rotate List
- 61. Rotate List
- 61. Rotate List
- 61. Rotate List LeetCode
- leetcode 61. Rotate List
- 61. Rotate List
- 61. Rotate List
- 61. Rotate List
- css预处理语言的模块化实践
- 帝国cms模板标签大全_帝国cms常用标签调用方法总结
- linux信号
- iostream/fstream中的输入输出流指针的绑定,tie函数的使用。
- slim学习小解
- 61. Rotate List
- 删除字符串中空格
- 进程
- Python通过paramiko连接Linux服务器
- linux磁盘分区fdisk命令详解
- 如何重建RAC的控制文件?
- Apache Prefork、Worker和Event三种MPM分析
- 简单理解js的prototype属性
- jquery序列化表单数据