Jan 19 - Rotate List; Linked List; Two Pointers; Iteration & Recursion;

Use 3 pointers each of them points to the address of Head, The node before Tail and Tail node; When rotating the list step by step, tail.next = head; tail_prev.next = null. New head = tail and new Tail = tail_prev;

Code: (Recursion way)

/**
 * 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 || head.next == null) return head;

        ListNode cur = head;
        int len = 0;
        while(cur != null && cur.next != null){
            cur = cur.next;
            len++;
        }
        if(cur != null) len++;
        k = k % len;
        if(k == 0) return head;
        ListNode tail = cur;
        ListNode newHead = rotateOneStep(head, tail, k);
        return newHead;
    }

    public ListNode rotateOneStep(ListNode head, ListNode tail, int num){
        ListNode tail_prev = head;
        while(tail_prev != null && tail_prev.next.next != null) tail_prev = tail_prev.next;
        tail.next = head;
        tail_prev.next = null;
        if(num == 1) return tail;
        head = tail;
        tail = tail_prev;
        return rotateOneStep(head, tail, num-1);
    }
}

  

Code(Iteration way):

public class Solution {
    //ListNode newHead, newTail;
    public ListNode rotateRight(ListNode head, int k) {
        if(head == null || head.next == null) return head;

        ListNode cur = head;
        int len = 0;
        while(cur != null && cur.next != null){
            cur = cur.next;
            len++;
        }
        if(cur != null) len++;
        k = k % len;
        ListNode tail = cur;
        for(int i = 0; i < k; i++){
            ListNode tail_prev = head;
            while(tail_prev != null && tail_prev.next.next != null) tail_prev = tail_prev.next;
            tail.next = head;
            tail_prev.next = null;
            head = tail;
            tail = tail_prev;
        }
        return head;
    }

  Code. Use List to store each node:

/**
 * 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 || head.next == null) return head;
        List<ListNode> findPrev = new ArrayList<>();
        ListNode cur = head;
        int len = 0;
        while(cur != null && cur.next != null){
            findPrev.add(cur);
            cur = cur.next;
            len++;
        }
        if(cur != null) {
            findPrev.add(cur);
            len++;
        }
        k = k % len;
        ListNode tail = cur;
        for(int i = 0; i < k; i++){

            ListNode tail_prev = findPrev.get(len-2);
            tail.next = head;
            tail_prev.next = null;
            head = tail;
            tail = tail_prev;
            findPrev.add(0, findPrev.remove(len-1));
        }
        return head;

    }
}

  

时间: 2024-08-27 00:34:46

Jan 19 - Rotate List; Linked List; Two Pointers; Iteration & Recursion;的相关文章

Jan 16 - Search Insert Position; Array; Binary Search; Iteration&amp;Recursion;---Iteration再补上

Recursion: 代码: public class Solution { public int searchInsert(int[] nums, int target) { int len = nums.length; if(len == 0) return 0; return findPosition(nums, 0, len-1, target); } public int findPosition(int[] nums, int start, int end, int target){

Jan 07 - Rotate Array

public class Solution { public void rotate(int[] nums, int k) { if(nums.length < 2) return; k = k % nums.length; if(k == 0) return; int tail = 0; for(int i = 1; i <= k; i++){ tail = nums[nums.length-1]; for(int j = 1; j < nums.length; j++){ nums[

Jan 17 - Rotate Image; 2D Array; xPos and yPos;

代码: public class Solution { public void rotate(int[][] matrix) { int n = matrix.length; for(int i = 0; i < n/2; i++){ int num = n-i*2; for(int j = 0; j < num-1; j++){ int temp = matrix[i][i+j]; matrix[i][i+j] = matrix[i+num-1-j][i]; matrix[i+num-1-j

Jan 19 - Permutation Sequence; BackTracking; Factorial;

Code: public class Solution { public static String getPermutation(int n, int k) { String result = ""; if(n == 1) return result + 1; int factorial_n = factorial(n-1); boolean[] isUsed = new boolean[n]; return addDigit(factorial_n, k-1, n-1, isUse

Jan 19 - Unique Paths; Array; DP;

first of all, we're goin to create a 2D array(int[][] grid) to store the number of paths to each position. we get the minimum value of m and n, mark it as min, then look through the row and column of grid[i][i] in each loop, when i == 0, we know we a

Jan 19 - Sqrt(x); Math; Binary Search;

We should keep in mind that never let the integer value overranged or overstacked.. Here is the trick, instead of comparing the square of a interger with the target value, we can compare the integer value i with the quotient x/i; if(i > x/i) high = i

Jan 10 - Reverse Linked List;Data Structure; Linked List; Pointer; Iteration &amp; Recursion

Iteration: 代码: /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { public ListNode reverseList(ListNode head) { if(head == null) return null; List

[LeetCode] Rotate List 单项链表旋转

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. Hide Tags Linked List Two Pointers 这题有点难理解,k 的意思是链表右起第k 个,k 大于链的个数时候

【LeetCode】234 - Palindrome Linked List

Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time and O(1) space? Hide Tags: Linked List Two Pointers 1 #include<iostream> 2 #include<vector> 3 using namespace std; 4 5 typedef struct ListNode