zoukankan      html  css  js  c++  java
  • Add Two Numbers

    代码:

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) {
     *         val = x;
     *         next = null;
     *     }
     * }
     */
    public class Solution {
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
                  ListNode result = null;
    
            ListNode temp = null, node = null;
            int digit = 0, bit = 0;
            while (l1 != null && l2 != null) {
                digit = l1.val + l2.val + bit;
                bit = digit / 10;
                digit = digit % 10;
                node = new ListNode(digit);
                if (result == null) {
                    result = node;
                    temp = result;
                } else {
                    temp.next = node;
                    temp = temp.next;
                }
                l1 = l1.next;
                l2 = l2.next;
            }
    
            while (l1 != null) {
                digit = l1.val + bit;
                bit = digit / 10;
                digit = digit % 10;
                node = new ListNode(digit);
                if (result == null) {
                    result = node;
                    temp = result;
                } else {
                    temp.next = node;
                    temp = temp.next;
                }
                l1 = l1.next;
            }
    
            while (l2 != null) {
                digit = l2.val + bit;
                bit = digit / 10;
                digit = digit % 10;
                node = new ListNode(digit);
                if (result == null) {
                    result = node;
                    temp = result;
                } else {
                    temp.next = node;
                    temp = temp.next;
                }
                l2 = l2.next;
            }
    
            if (bit != 0) {
                node = new ListNode(bit);
                if (result == null) {
                    result = node;
                    temp = result;
                } else {
                    temp.next = node;
                    temp = temp.next;
                }
            }
            return result;
        }
    }
  • 相关阅读:
    父亲对子女的话
    开通博客
    在linux下安装MySQLdb及基本操作
    java 词汇表速查手册
    java数据源的几种配置
    DBCP的参数配置
    Linux crontab定时执行任务
    很好看的Button CSS.
    C# 创建活动目录.txt
    解密存储过程
  • 原文地址:https://www.cnblogs.com/xiaoxian1369/p/3631974.html
Copyright © 2011-2022 走看看