• LeetCode_双指针_中等_61. 旋转链表


    1.题目

    给你一个链表的头节点 head ,旋转链表,将链表每个节点向右移动 k 个位置。

    示例 1:
    在这里插入图片描述
    输入:head = [1,2,3,4,5], k = 2
    输出:[4,5,1,2,3]

    示例 2:
    在这里插入图片描述
    输入:head = [0,1,2], k = 4
    输出:[2,0,1]

    提示:
    链表中节点的数目在范围 [0, 500] 内
    -100 <= Node.val <= 100
    0 <= k <= 2 * 109

    来源:力扣(LeetCode)
    链接:https://leetcode.cn/problems/rotate-list

    2.思路

    (1)双指针
    详细分析过程见下面代码中的注释。

    3.代码实现(Java)

    //思路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 (k == 0 || head == null || head.next == null) {
                return head;
            }
            
            //计算链表长度
            int length = 0;
            ListNode aux = head;
            while (aux != null) {
                aux = aux.next;
                length++;
            }
            
            // 当 k > length 时,其旋转结果与 k = k % length 一样
            k = k % length;
            if (k == 0) {
            	// k % length 如果等于 0,则说明 length 是 k 的整数倍,旋转之后结果与原来相同,故直接返回 head
                return head;
            }
            
            /*
            	设 n = length
            	V1 -> V2 -> ... -> V(n-k-1) -> V(n-k) -> ... -> Vn -> null
            	将链表每个节点向右移动 k 个位置之后,得到:
            	V(n-k) -> ... -> Vn -> V1 -> V2 -> ... -> V(n-k-1) -> null
            	所以只需要先找到找到节点 V(n-k-1)、V(n-k) 以及 Vn,然后令
            	V(n-k-1).next = null
            	Vn.next = V1(即head)
            	最后返回 V(n-k) 即可
            	V(n-k-1)、V(n-k) 以及 Vn 分别对应下面代码中的 lastNode、newHead 和最后一个while循环结束后的 aux
            */
            aux = head;
            ListNode lastNode = head;
            int tmp = length - k;
            while (tmp > 0) {
                aux = aux.next;
                if (tmp != 1) {
                    lastNode = lastNode.next;
                }
                tmp--;
            }
            lastNode.next = null;
            ListNode newHead = aux;
            while (aux.next != null) {
                aux = aux.next;
            }
            aux.next = head;
            return newHead;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
  • 相关阅读:
    人工蜂群(ABC)算法附matlab代码
    vue项目上线后去除控制台所有console.log打印-配置说明
    leetcode - 1428. Leftmost Column with at Least a One
    数据结构-作业9
    微服务·架构组件之网关
    【祝福伟大的祖国】Java Web 9 Request&Response 9.2.1 Request 继承体系
    应用实践 | 10 亿数据秒级关联,货拉拉基于 Apache Doris 的 OLAP 体系演进
    Vue48-ref属性
    线程的状态
    【微信小程序】小程序代码基本组成结构
  • 原文地址:https://blog.csdn.net/weixin_43004044/article/details/125633078