zoukankan      html  css  js  c++  java
  • LeetCode 61. Rotate List 题解

    Total Accepted: 71150 Total Submissions: 308672 Difficulty: Medium
    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.

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public ListNode rotateRight(ListNode head, int k) {
            ListNode pNote = head;
            ListNode qNote = head;
            int length = 0;
            
            if(head == null || k == 0){  //如果只有一个head,则返回head
                return head;
            }
    
    //pNote从head开始遍历整个链表,length记录表长度,length超过n之后,qNote开始遍历。这样能保证qNode是需要寻找的节点的前一个节点
    
            while(pNote.next != null){ 
                length ++;    
                pNote = pNote.next;
                if (length > k){
                    qNote = qNote.next;
                }
    
            }
            
            length = length + 1;
            
            // 根据k和length的大小进行判断
            if ( k > length) {//递归求解
                
                k = k % length;
                return rotateRight(head, k);
                
            } else  if ( k == length){
                
                return head;
                
            } else {
               
                ListNode newhead = qNote.next;
                qNote.next = null;
                pNote.next = head;
        
                return newhead;
            }
            
          
        }
    }
    
  • 相关阅读:
    P1744 采购特价商品
    P1359 租用游艇
    P3092 [USACO13NOV]没有找零No Change
    P1272 重建道路
    P2014 选课
    P1026 统计单词个数
    P1776 宝物筛选_NOI导刊2010提高(02)
    自定义异步非阻塞tornado框架
    tornado进阶篇
    tornado基础篇
  • 原文地址:https://www.cnblogs.com/liveandlearn/p/5567858.html
Copyright © 2011-2022 走看看