首页 > 其他 > 详细

369. Plus One Linked List

时间:2016-07-17 14:07:39      阅读:404      评论:0      收藏:0      [点我收藏+]

技术分享

    /*
     * 369. Plus One Linked List
     * 2016-7-16 by Mingyang
     * 直接先reverse,做完了再reverse,屌丝代码,未优化,就这么样吧,哈哈
     */
    public static ListNode plusOne(ListNode head) {
        if(head==null)
          return null;
        head=reverseList(head);
        ListNode pre=new ListNode(-1);
        pre.next=head;
        ListNode run=pre;
        boolean isOwn=false;
        while(run.next!=null){
            if(run.next.val==9){
                run.next.val=0;
                run=run.next;
                isOwn=true;
            }else{
               isOwn=false;
               run.next.val=run.next.val+1;
               break;
            }
        }
        if(isOwn){
            run.next=new ListNode(1);
        }
        head=reverseList(pre.next);
        return head;
    }

 

369. Plus One Linked List

原文:http://www.cnblogs.com/zmyvszk/p/5677727.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!