LeetCode61、旋转链表

题目描述

https://leetcode-cn.com/problems/rotate-list/
在这里插入图片描述

解法

首先一看到这个题,我们就可以想到的想法:

  • k的次数大过链表的长度
  • k的次数小于链表的长度

对应的做法。

从本质上看,旋转一次就是:

  • 找到尾部结点和其上一个结点
  • 去除尾部结点,然后将该尾部结点头插法

那么旋转K次就是进行旋转一次的K次调用:

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;
    }
}

在这里插入图片描述
明显,会出现超时,那么为什么呢?因为旋转的次数大于链表的长度,旋转得多了,可能变回原来的样子,或者只是在原来的样子上旋转一次或者两次。所以要对K进行一个处理:

  • K==len(head),则必然是原来的样子,即旋转K%len(head)次数.
  • K>len(head),只需要旋转的次数是K-len(head),那如果K-len(head)>len(head),只需要旋转K-len(head)-len(head),即K%len(head)次数.

于是我们得到:

/**
 * 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;
    }
}

最差情况下的复杂度为:O(kN),k<N,N为链表结点数。
在这里插入图片描述

我以为我行了,没想到啊,官方题解给了我当头一棒:

在这里插入图片描述

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)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

时间复杂度O(n),空间复杂度O(1)。

猜你喜欢

转载自blog.csdn.net/qq_44861675/article/details/115260604
今日推荐