LeetCode61, rotating linked list

Title description

https://leetcode-cn.com/problems/rotate-list/
Insert picture description here

solution

First of all, when we see this question, we can think of ideas:

  • The number of times k is greater than the length of the linked list
  • The number of k is less than the length of the linked list

The corresponding approach.

Essentially, one rotation is:

  • Find the tail node and the previous node
  • Remove the tail node, and then nod and insert the tail node

Then rotate K times is to make K calls of one rotation:

class Solution {
    
    
    public ListNode rotateRight(ListNode head, int k) {
    
    
      //K表示旋转次数而已
      ListNode first=head;
    while(k>0){
    
    
        k--;
         first = rotateRight(first);
    }
    return first;
      //实际上就是尾结点的去除,然后头插入
    }
    public ListNode rotateRight(ListNode head){
    
    
        if(head==null||head.next==null) return head;
        ListNode tail = head;
        ListNode first = head;
        ListNode pre_tail = null;//尾结点的上一个
        while(head.next!=null){
    
    
            pre_tail = head;
            tail = head.next;
            head = tail;
        }
        //旋转操作就是去除尾部结点
        pre_tail.next = null;
        //头插法
        tail.next = first;
        return tail;
    }
}

Insert picture description here
Obviously, there will be a timeout, so why? Because the number of rotations is greater than the length of the linked list, the rotation is much longer, and it may change back to the original appearance, or just rotate once or twice on the original appearance. So we have to deal with K:

  • K==len(head), it must be the original appearance, that is, the number of rotations K%len(head).
  • K>len(head), only need to rotate the number of times is K-len(head), then if K-len(head)>len(head), only need to rotate K-len(head)-len(head), that is K%len(head) times.

So we get:

/**
 * 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;//避免len=0
      //K表示旋转次数而已
      ListNode first=head;
      ListNode temp = head;
      int len=0;
      while(temp!=null){
    
    
          len++;
          temp = temp.next;
      }
      k = k%len;
    while(k>0){
    
    
        k--;
         first = rotateRight(first);
    }
    return first;
      //实际上就是尾结点的去除,然后头插入
    }
    public ListNode rotateRight(ListNode head){
    
    
        if(head==null||head.next==null) return head;
        ListNode tail = head;
        ListNode first = head;
        ListNode pre_tail = null;//尾结点的上一个
        while(head.next!=null){
    
    
            pre_tail = head;
            tail = head.next;
            head = tail;
        }
        //旋转操作就是去除尾部结点
        pre_tail.next = null;
        //头插法
        tail.next = first;
        return tail;
    }
}

The worst-case complexity is: O(kN), k<N, N is the number of linked list nodes.
Insert picture description here

I thought I was doing it, but I didn’t expect that the official solution gave me a blow:

Insert picture description here

class Solution {
    
    
    public ListNode rotateRight(ListNode head, int k) {
    
    
        if (k == 0 || head == null || head.next == null) {
    
    
            return head;
        }
        int n = 1;
        ListNode iter = head;
        while (iter.next != null) {
    
    
            iter = iter.next;
            n++;
        }
        int add = n - k % n;
        if (add == n) {
    
    
            return head;
        }
        iter.next = head;
        while (add-- > 0) {
    
    
            iter = iter.next;
        }
        ListNode ret = iter.next;
        iter.next = null;
        return ret;
    }
}

作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/rotate-list/solution/xuan-zhuan-lian-biao-by-leetcode-solutio-woq1/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

The time complexity is O(n), and the space complexity is O(1).

Guess you like

Origin blog.csdn.net/qq_44861675/article/details/115260604