zoukankan      html  css  js  c++  java
  • [LeetCode]Rotate List

    题目:Rotate List

    从第k个位置旋转链表。

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

    思路1:(16ms)

    1.求链表长度,将k取模,保证k小于链表长度;

    2.遍历链表找到k的位置,然后逆置前k个链表节点;

    3.交换前后链表顺序实现旋转。

    思路2(15ms)

    1.求链表长度,将k取模,保证k小于链表长度;

    2.建立和链表等长的数组,将链表中的数据从k处开始复制到数组中;

    3.在从链表头开始将数组中元素复制到链表中,即可实现上述旋转。

    package com.example.medium;
    
    public class RotateRight {
    
        private class ListNode {
            int val;
            ListNode next;
            ListNode(int x) { val = x; }
        }
    
            //思路2
        private ListNode rotateRight2(ListNode head, int k) {
            if(k < 0)return head;
               int length = 0;
            ListNode p = head;
                while(p != null){
                    p = p.next;
                    length++;
                }
                if(k >= length && length != 0)k = k%length;
                if(k == 0 || length == 0)return head;
                int values[] = new int[length];//定义一个数组,从数组的第k个位置开始复制链表的值
                p = head;
                while(p != null){//复制链表元素到数组
                    values[k++] = p.val;
                    if(k >= length)k = 0;//k超过数组尾部时,从零开始
                    p = p.next;
                }
                p = head;
                k = 0;
                while(p != null){//再将数组的值复制回链表
                    p.val = values[k++];
                    p = p.next;
                }
            return head;
        }
    
            //思路1
        private ListNode rotateRight(ListNode head, int k) {
                //int values[] = new int[k];
                if(k < 0)return head;
                int length = 0;
                ListNode p = head;
                while(p != null){//求链表长度
                    p = p.next;
                    length++;
                }
                p = head;
                if(k >= length && length != 0)k = k%length;//求模去掉多余长度
                if(k == 0)return head;
                for(int i = 0;i < k && p != null;i++)p = p.next;//找到k的位置
                if(p == null)return head;//k > head.length
                ListNode q = head;
                while(p.next != null){//逆置链表
                    p = p.next;
                    q = q.next;
                }
                p.next = head;
                head = q.next;
                q.next = null;
                return head;
        }
        
        public void display(){
                int n = 5;
                ListNode head = null;
                for(int i = n;i > 0;i--){
                    ListNode p = new ListNode(i);
                    p.next = head;
                    head = p;
                }
                ListNode p = head;
                while(p != null){
                    System.out.println(p.val);
                    p = p.next;
                }
                p = rotateRight2(head,7);
        }
    
        public static void main(String args[]){
            new RotateRight().display();
        }
    }
  • 相关阅读:
    《RabbitMQ 实战》读书笔记
    使用jstack命令查看CPU高占用的问题记录
    两种常见的单元测试方式(笔记)
    Apache Solr入门教程(转)
    搜索引擎选择: Elasticsearch与Solr(转)
    CopyOnWriteArrayList与Collections.synchronizedList的性能对比(转)
    理解list和vector的区别
    从上往下打印出二叉树的每个节点,同层节点从左至右打印。
    TypeError: Object function (req, res, next) { app.handle(req, res, next); } has no method 'configure'
    Cannot find module 'crc'
  • 原文地址:https://www.cnblogs.com/yeqluofwupheng/p/6684840.html
Copyright © 2011-2022 走看看