zoukankan      html  css  js  c++  java
  • Rotate List

    Rotate List

    问题:

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

    思路:

      简单的数学推导问题

    我的代码:

    public class Solution {
        public ListNode rotateRight(ListNode head, int n) {
            if(head == null || n <= 0) return head;
            int len = 0;
            ListNode dummy = new ListNode(-1);
            dummy.next = head;
            ListNode tail = dummy;
            ListNode cur = head;
            while(cur != null)
            {
                cur = cur.next;
                tail = tail.next;
                len++;
            }
            int gap = len - n%len;
            ListNode last = dummy;
            for(int i = 0; i < gap; i++)
            {
                last = last.next;
            }
            tail.next = dummy.next;
            dummy.next = last.next;
            last.next = null;
            return dummy.next;        
        }
    }
    View Code
  • 相关阅读:
    代理模式
    适配器模式
    策略模式
    原型模式
    内存溢出
    jvm常用参数
    单例模式
    抽象工厂
    工厂方法模式
    选择器代码
  • 原文地址:https://www.cnblogs.com/sunshisonghit/p/4335474.html
Copyright © 2011-2022 走看看