369. Plus One Linked List

    /*
     * 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;
    }

 

相关文章: