Leetcode 61. Rotate List
方法1: 每次向右移動一次,但是這個方法tle。時間複雜度n平方。
/**
* 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; }
* }
*/
// recursion & iterate
class Solution {
public ListNode rotateRight(ListNode head, int k) {
if(head == null || head.next ==null) return head;
ListNode temp = head;
while(k > 0){
temp = rotateOneRight(temp);
k--;
}
return temp;
}
private ListNode rotateOneRight(ListNode head){
ListNode curr = head;
ListNode prev = new ListNode(0);
while(curr.next != null){
prev = curr;
curr = curr.next;
}
prev.next = null;
curr.next = head;
return curr;
}
}
方法2: lc官方解答,有點類似數學,需要推算出移動過後新head和新tail的位置。時間複雜度n,空間複雜度1.
/**
* 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 || head.next == null) return head;
ListNode curr = head;
int count = 1;
while(curr.next != null){
curr = curr.next;
count++;
}
curr.next = head;
int newTailPosition = count - k % count - 1;
int newHeadPosition = count - k;
ListNode new_tail = head;
for(int i = 0; i < newTailPosition; ++i){
new_tail = new_tail.next;
}
ListNode new_head = new_tail.next;
new_tail.next = null;
return new_head;
}
}
總結:
- 還是比較經典的node移動問題,非常鍛鍊思維,不錯的題目。
相關文章
- [LeetCode] 61. Rotate ListLeetCode
- [LeetCode] Rotate StringLeetCode
- Rotate Array@LeetCodeLeetCode
- 【Leetcode】61.旋轉連結串列LeetCode
- [LeetCode] 148. Sort ListLeetCode
- LeetCode | 148. Sort ListLeetCode
- LeetCode | 141 linked list cycleLeetCode
- LeetCode | 147. Insertion Sort ListLeetCode
- [leetcode]linked-list-cycle-iiLeetCode
- Leetcode 206. Reverse Linked ListLeetCode
- Leetcode 234. Palindrome Linked ListLeetCode
- LeetCode 382 Linked List Random NodeLeetCoderandom
- canvas rotate()Canvas
- LeetCode之Odd Even Linked List(Kotlin)LeetCodeKotlin
- [LeetCode] 328. Odd Even Linked ListLeetCode
- Leetcode 142. Linked List Cycle IILeetCode
- LeetCode - Easy - 206. Reverse Linked ListLeetCode
- Leetcode 203. Remove Linked List ElementsLeetCodeREM
- Leetcode 237. Delete Node in a Linked ListLeetCodedelete
- LeetCode 138. Copy List with Random PointerLeetCoderandom
- LeetCode | 203. Remove Linked List ElementsLeetCodeREM
- [LeetCode] 430. Flatten a Multilevel Doubly Linked ListLeetCode
- Leetcode 19 Remove Nth Node From End of ListLeetCodeREM
- [LeetCode] 2487. Remove Nodes From Linked ListLeetCodeREM
- LeetCode 83. Remove Duplicates from Sorted ListLeetCodeREM
- 61. 旋轉連結串列
- [LeetCode] 109. Convert Sorted List to Binary Search TreeLeetCode
- Java for LeetCode 109 Convert Sorted List to Binary Search TreeJavaLeetCode
- 48. Rotate Image
- CSS3 rotate()CSSS3
- LeetCode Remove Nth Node From End of List(019)解法總結LeetCodeREM
- LeetCode707:設計連結串列 Design Linked ListLeetCode
- Leetcode(Python3) 19. Remove Nth Node From End of ListLeetCodePythonREM
- LeetCode 83.Remove Duplicates from Sorted List(從已排序連結串列中除去重複) Easy/Linked ListLeetCodeREM排序
- 資料結構與演算法 | Leetcode 206:Reverse Linked List資料結構演算法LeetCode
- 資料結構與演算法 | Leetcode 141:Linked List Cycle資料結構演算法LeetCode
- 資料結構與演算法 | Leetcode 876. middle-of-the-linked-list資料結構演算法LeetCode
- CSS rotate3d(x,y,z,angle)CSS3D