[Leetcode] Rotate List (Java)
来源:互联网 发布:linux crm 命令 编辑:程序博客网 时间:2024/06/16 09:53
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->NULL
and k = 2
,
return 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 { public ListNode rotateRight(ListNode head, int n) {if(head==null)return head;int num = 0;ListNode res = new ListNode(-1);res.next = head;while(head!=null){num++;head=head.next;}n%=num;n=num-n;if(n==0)return head;head = res.next;int i=1;while(i<n){head=head.next;i++;}ListNode pre = new ListNode(-1);pre.next = head.next;if(pre.next==null)return res.next;head.next=null;head=pre.next;while(head.next!=null)head=head.next;head.next=res.next;return pre.next; }}
0 0
- [Leetcode] Rotate List (Java)
- [LeetCode][Java] Rotate List
- leetcode:Rotate List 【Java】
- Rotate List leetcode java
- (Java)LeetCode-61. Rotate List
- 【leetcode】61. Rotate List【java】
- [LeetCode] 61. Rotate List java
- [leetcode][list] Rotate List
- [leetcode]61. Rotate List@Java解题报告
- LeetCode: Rotate List
- LeetCode Rotate List
- LeetCode : Rotate List
- [Leetcode] Rotate List
- [LeetCode] Rotate List
- [leetcode] Rotate List
- leetcode 70: Rotate List
- [Leetcode] Rotate List
- 【leetcode】Rotate List
- android面试题及答案
- Linux学习笔记 - 檢驗軟體正確性
- wxpython绘图例子(避免窗口闪烁)
- 系统的回车换行
- Qt document
- [Leetcode] Rotate List (Java)
- 函数调用约定
- 新的开始
- FusionCharts 3D柱状图
- ubuntu 13.10 LAMP + PhpMyAdmin 配置
- 有无奈有快乐的银行系统----My Love.————娜猫
- Android开源框架AsyncHttpClient (android-async-http)使用
- win7系统文件夹无法显示韩文_解决显示方块□□□的问题
- PHP设计模式之:单例模式