【Leetcode】61. Rotate List

Given a list, rotate the list to the right by k places, where k is non-negative.

Example:

Given 1->2->3->4->5->NULL and k = 2,

return 4->5->1->2->3->NULL.

Tips:右移结点,过程如下:

k=2,右移两次:

①5->1->2->3->4

②4->5->1->2->3

思路:(1)实例化一个fast指针,使其等于head结点,使fast指针先向后移动k次。

(2)创建一个slow指针,然后两个指针一起向后移动,直到fast.next!=null,停止。

(3)k的值可能大于链表总长度,需要对k取余,k = k%count;

(4)使fast.next等于head,slow的下一个结点作为新的头结点。

package medium;

import dataStructure.ListNode;

public class L61RotateList {
    /*
     * Given a list, rotate the list to the right by k places, where k is
     * non-negative.
     *
     * Example:
     *
     * Given 1->2->3->4->5->NULL and k = 2,
     *
     * return 4->5->1->2->3->NULL.
     *
     */
    public ListNode rotateRight(ListNode head, int k) {
        if(head==null || k<0)return null;
        ListNode node = new ListNode(0);
        node.next = head;
        ListNode fast=head;
        ListNode newHead=head;
        int count=0;
        while(newHead!=null){
            count++;
            newHead=newHead.next;
        }
        if(k > count)
               k = k%count;

        for(int i=0;i<k;i++){
            fast=fast.next;
        }
        if(fast==null){
            return node.next;
        }else{
            ListNode slow=head;
            while(fast.next!=null){
                slow=slow.next;
                fast=fast.next;
            }
            fast.next=head;
            ListNode cur=slow.next;
            node.next=cur;
            slow.next=null;
        }
        return node.next;
    }

    public static void main(String[] args) {
        ListNode head1 = new ListNode(1);
        ListNode head2 = new ListNode(2);
        ListNode head3 = new ListNode(3);
        ListNode head4 = new ListNode(4);
        ListNode head5 = new ListNode(5);
        ListNode head6 =  null;
        head1.next = head2;
        head2.next = head3;
        head3.next = head4;
        head4.next = head5;
        head5.next = head6;
        L61RotateList l61=new L61RotateList();
        int k=2;
        ListNode node=l61.rotateRight(head1, k);
        while(node!=null){
            System.out.println(node.val);
            node=node.next;
        }
        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~");
        ListNode h1 = new ListNode(1);
        ListNode h2=new ListNode(2);
        h1.next=h2;
        h2.next=null;
        ListNode node1=l61.rotateRight(h1, 3);
        while(node1!=null){
            System.out.println(node1.val);
            node1=node1.next;
        }
    }
}

原文地址:https://www.cnblogs.com/yumiaomiao/p/8448467.html

时间: 2024-10-24 11:51:25

【Leetcode】61. Rotate List的相关文章

【一天一道LeetCode】#61. Rotate List

一天一道LeetCode系列 (一)题目 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. (二)解题 本题的思路: 1.找到倒数第K个节点 2.将k以后的节点移动到前面来,与头结点

【LeetCode】189. Rotate Array 解题小结

题目: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. 这道题目 有很多种解法,我用的是这种,相对也好理解,先所有元素reverse,再0-(n-k)的元素reverse,然后(n-k)-n的元素reverse. class Solution { pub

【LeetCode】048. Rotate Image

题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Note:You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do

【LeetCode】189. Rotate Array

Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. Note:Try to come up as many solutions as you can, there are at least 3 different ways to s

【Leetcode】Rotate Image

You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up: Could you do this in-place? 思路:第一种思路是一层一层的进行旋转,比较直观:第二种思路则比较取巧,首先沿着副对角线翻转一次,然后沿着水平中线翻转一次. 代码一: class Solution { public: void rotate(vector<

【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. start with an example. Given [0,1,2], rotate 1 steps to the right -&

【leetcode】Reverse Words in a String

问题:给定一个字符串,字符串中包含若干单词,每个单词间由空格分隔,将单词逆置,即第一个单词成为最后一个单词,一次类推. 说明:字符串本身可能包含前导空格或后导空格,单词间可能包含多个空格,要求结果中去掉前导和后导空格,单词间空格只保留一个. 与rotate函数类似,先逆置每个单词,再将所有字符串逆置. void reverseWords(string &s) { if(s.size() == 0) return; char blank = ' '; size_t len = s.size();

【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 递归和非递归,此提比较简单.广度优先遍历即可.关键之处就在于如何保持访问深度. 下面是4种代码: 1

【leetcode】Generate Parentheses

题目: 给定整数n,返回n对匹配的小括号字符串数组. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 分析: 这种问题的模式是:1)问题的解有多个 ,2)每个解都是由多个有效的 "步骤" 组成的,3)变更以有解的某个或某些"步骤"