My code:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode rotateRight(ListNode head, int k) {
if (head == null || k < 0)
return head;
else if (head.next == null)
return head;
ListNode dummy = new ListNode(Integer.MIN_VALUE);
dummy.next = head;
ListNode tail = head;
int total = 1;
while (tail.next != null) {
tail = tail.next;
total++;
}
k = k % total;
if (k == 0)
return dummy.next;
ListNode curr = dummy;
for (int i = 0; i < (total - k); i++)
curr = curr.next;
if (curr.next == null)
return dummy.next;
ListNode newHead = curr.next;
curr.next = null;
tail.next = dummy.next;
dummy.next = newHead;
return dummy.next;
}
public static void main(String[] args) {
Solution test = new Solution();
ListNode n1 = new ListNode(1);
ListNode n2 = new ListNode(2);
ListNode n3 = new ListNode(3);
ListNode n4 = new ListNode(4);
ListNode n5 = new ListNode(5);
n1.next = n2;
n2.next = n3;
n3.next = n4;
n4.next = n5;
System.out.println(test.rotateRight(n1, 3));
}
}
My test result:
这次题目也不难。先遍历一遍找出总个数 total
然后根据 total - k 找到那个断裂点。断开。在用dummy和新的头接上。
原来的头接到tail后面去,就差不多了。
主要我是没能理解, k >= total 是什么意义。
查了之后发现,做一个 k = k % total 处理就行了。
**
总结: LinkedList rotate
**
Anyway, Good luck, Richardo!
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode rotateRight(ListNode head, int k) {
if (head == null)
return null;
int n = 1;
ListNode temp = head;
ListNode tail = head;
while (temp.next != null) {
n++;
temp = temp.next;
tail = temp;
}
k = k % n;
if (k == 0)
return head;
temp = head;
for (int i = 1; i < n - k; i++) {
temp = temp.next;
}
ListNode newHead = temp.next;
temp.next = null;
tail.next = head;
return newHead;
}
public static void main(String[] args) {
ListNode n1 = new ListNode(1);
ListNode n2 = new ListNode(2);
ListNode n3 = new ListNode(3);
ListNode n4 = new ListNode(4);
ListNode n5 = new ListNode(5);
n1.next = n2;
n2.next = n3;
n3.next = n4;
n4.next = n5;
Solution test = new Solution();
ListNode head = test.rotateRight(n1, 2);
ListNode temp = head;
while (temp != null) {
System.out.print(temp.val + "->");
temp = temp.next;
}
}
}
有一个 corner case没有考虑到,当k= 0 时,这个时候是不需要旋转的,如果强制旋转, newHead = tail.next -> null. 会出错。
Anyway, Good luck, Richardo!
My code:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode rotateRight(ListNode head, int k) {
if (k < 0 || head == null) {
return null;
}
ListNode tail = head;
int total = 0;
while (tail.next != null) {
total++;
tail = tail.next;
}
total++;
k = k % total;
if (k == 0) {
return head;
}
int counter = total - k - 1;
ListNode curr = head;
while (counter > 0) {
curr = curr.next;
counter--;
}
ListNode newHead = curr.next;
curr.next = null;
tail.next = head;
return newHead;
}
}
k = 0 的时候,不需要rotate,直接返回就行。
Anyway, Good luck, Richardo! -- 08/16/2016