zoukankan      html  css  js  c++  java
  • Rotate List 面试题

    今天做了leetcode的Rotate List,刚开始头脑不清楚,写的乱七八糟的,后来改了下,提交了,能过,把代码贴出来。

    做题的时候头脑要清楚,我刚开始做完的时候才发现我把向左向右移动弄反了,后来修改了下。

     1 public static ListNode rotateRight(ListNode head, int n) {
     2         ListNode first=head;
     3         ListNode second=head;
     4         ListNode result=null;
     5         ListNode t=null;
     6         int ln=0;
     7         if(head==null)
     8             return result;
     9         while(head.next!=null){
    10             ln++;
    11             head=head.next;
    12         }
    13         ln++;//求出list长度
    14         n=n%ln;
    15         n=ln-n;//求出向右移动距离
    16         if(n==0)
    17             return first;
    18         
    19         while(n>1){
    20             if(first.next!=null)
    21                 first=first.next;
    22             else 
    23                 first=head;
    24             n--;
    25         }
    26         t=first;
    27         if(t.next==null)
    28             return second;
    29         else{
    30             result=first.next;
    31             t.next=null;
    32             first=result;
    33         }//将移动后的链表的尾巴确定        
    34         while(first.next!=null){
    35             first=first.next;
    36         }
    37         first.next=second;//将链表头与尾连起来
    38         return result;        
    39     }
  • 相关阅读:
    Additional Color Tables
    How to make a non-symmetric color pallet around zero
    【cl】cmd相关命令
    【cl】oracle之Sequence
    常用命令之ps
    卸载oracle
    oracle—无法启动
    oracle 导入数据
    proc文件系统漫谈
    GStreamer插件分类
  • 原文地址:https://www.cnblogs.com/weilq/p/3622731.html
Copyright © 2011-2022 走看看