码迷,mamicode.com
首页 > 其他好文 > 详细

[jeetcode] add two numbers

时间:2014-10-19 21:14:25      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   ar   java   for   sp   div   

package jeetcode;

import java.util.HashMap;

public class jeetCode {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        /**
         * Definition for singly-linked list. public class ListNode { int val;
         * ListNode next; ListNode(int x) { val = x; next = null; } }
         */

        class ListNode {
            int val;
            ListNode next;

            ListNode(int x) {
                val = x;
                next = null;
            }
        }

        ListNode l1 = new ListNode(1);
        ListNode l2 = new ListNode(9);
        l2.next = new ListNode(9);

        ListNode l3 = new ListNode(0);
        ListNode p = l3;
        int jinwei = 0;
        while (l1 != null || l2 != null || jinwei != 0) {
            int plus = ((l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + jinwei);
            p.val = plus % 10;
            jinwei = plus / 10;
            ListNode now = new ListNode(0);

            l1 = l1 == null ? null : l1.next;
            l2 = l2 == null ? null : l2.next;

            if (l1 != null || l2 != null || jinwei != 0) {
                p.next = now;
                p = now;
            } else {
                p.next = null;
            }
        }
        while (l3 != null) {
            System.out.println(l3.val);
            l3 = l3.next;
        }

    }
}

 

[jeetcode] add two numbers

标签:style   blog   color   io   ar   java   for   sp   div   

原文地址:http://www.cnblogs.com/bluedreamviwer/p/jeetcode.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!