作业感受
题目地址: https://leetcode-cn.com/leetbook/read/linked-list/f00a2/
其实这一看这个作业很简单,但是做的过程中发现一个很tricky的问题就是k的处理,一开始我的思路局限在k小于链表size的世界里,提交后发现错误了。后来我修改了代码,虽然正确了,但是用时很长啊。
本来想写一个递归的,但是思路还是没有理清,就用了双指针。用时长大概是因为循环了两次的原因。
作业代码
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode rotateRight(ListNode head, int k) {
if(head == null) return null;
if(head.next == null) return head;
if(k == 0) return head;
ListNode dummyHead = new ListNode();
ListNode slow = head, fast = head;
while(k > 0){
fast = fast.next;
k--;
if(fast == null && k > 0) fast = head;
if(fast == null && k == 0) return head;
}
while(slow.next != null && fast.next != null){
slow = slow.next;
fast = fast.next;
}
dummyHead.next = slow.next;
fast.next = head;
slow.next = null;
return dummyHead.next;
}
}
作业表现
1359ms…真的是心碎啊!我猜测应该是有测试用例给了一个超大的k, 于是我决定优化第一个循环。
优化后的代码
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode rotateRight(ListNode head, int k) {
if(head == null) return null;
if(head.next == null) return head;
ListNode dummyHead = new ListNode();
ListNode slow = head, fast = head;
int size = 0;
while(fast != null){
fast = fast.next;
size++;
}
fast = head;
k = k % size;
if(k == 0) return head;
while(k > 0){
fast = fast.next;
k--;
}
while(slow.next != null && fast.next != null){
slow = slow.next;
fast = fast.next;
}
dummyHead.next = slow.next;
fast.next = head;
slow.next = null;
return dummyHead.next;
}
}
果然被我猜中了,k的上限是一个很大的数,node的个数上线只是500, 看来循环条件的选择很重要啊,不然真的是伤不起啊。优化以后用时降到1ms。