您的位置:首页 > 其它

IE6的3PX(像素)BUG,造成文字溢出

2013-03-12 20:17 281 查看
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.

1. 首先,需要先遍历一遍拿到这个list的长度,顺便把最后一个元素的next指向第一个head,做成一个循环链表。

2. 第二遍遍历,走到第length-k个元素,记录下下一个元素为要返回的元素,然后把这个元素的next设置为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(n==0||head==null){
return head;
}
// get length
int length = 0;
ListNode cursor = head;
ListNode tail = null;
while(cursor!=null){
length++;
tail = cursor;
cursor = cursor.next;
}
n = n%length;
tail.next = head;
int i=0;
cursor = head;
while(i<length-n){
tail = cursor;
cursor = cursor.next;
i++;
}
tail.next=null;

return cursor;
}
}
本文出自 “在云端” 博客,请务必保留此出处http://kcy1860.blog.51cto.com/2488842/1335660
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: