LeetCode2.2.6(Rotate List)
来源:互联网 发布:软件资源网站 编辑:程序博客网 时间:2024/05/22 05:22
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->nullptr and k = 2, return 4->5->1->2->3->nullptr.
这道题思路很巧妙,将尾节点next 指针指向首节点,形成一个环,接着往后跑len-k 步,从这里断开,就是要求的结果了。在实际中,我们选用两个指针,一个比另一个滞后K位即可。
public static void solution_2_2_6(Node head,int k){Node p1=head,p2=head;for(int i=1;i<=k;i++){p2=p2.next;}while(p2.next!=null){p1=p1.next;p2=p2.next;}p2.next=head;Node h=p1.next;p1.next=null;for(Node r=h;r!=null;r=r.next){System.out.print(r.data+" ");}}
0 0
- LeetCode2.2.6(Rotate List)
- Leetcode2: Rotate Array
- LeetCode2.1.16(Rotate Image)
- LeetCode2.2.3(Partition 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
- 在Ubuntu上安装Node.js的Upstream版本
- android windows 32位开发工具 eclipse+sdk23+adt23+ndk+genymotion插件
- JAVA生成给定范围内的不重复的随机数的几种方法
- 梯度上升法,梯度下降法
- servlet文件上传
- LeetCode2.2.6(Rotate List)
- CodeForces 387B George and Round
- 服务器维护手记-crontab引起大量sendmail进程打死内存问题
- Android Animation 动画的使用方法和介绍
- leetcode 069 Sqrt(x)(难易度:Medium)
- 1031 -- 素数求和问题
- 1032 -- 水仙花数
- Android Animation 动画的使用方法和介绍
- 1033 -- 鸡兔同笼