zoukankan      html  css  js  c++  java
  • Java解法-两数相加(Add Two Numbers)

      问题

      给出两个非空的链表用来表示两个非负的整数。其中,它们各自的位数是按照逆序的方式存储的,并且它们的每个节点只能存储一位数字。

     如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。

     您可以假设除了数字 0 之外,这两个数都不会以 0开头。

      Give two non-empty linked lists to represent two non-negative integers. Among them, their respective digits are stored in reverse order, and each node of them can   only store one digit.

      If we add these two numbers together, we will return a new linked list to represent their sum.

      You can assume that except for the number 0, neither of these numbers will start with 0.

      

       示例:

        输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
        输出:7 -> 0 -> 8
        原因:342 + 465 = 807


    Java解法1:
    public class Solution {
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            ListNode dummy = new ListNode(-1);
            ListNode cur = dummy;
            int carry = 0;
            while (l1 != null || l2 != null) {
                int d1 = l1 == null ? 0 : l1.val;
                int d2 = l2 == null ? 0 : l2.val;
                int sum = d1 + d2 + carry;
                carry = sum >= 10 ? 1 : 0;
                cur.next = new ListNode(sum % 10);
                cur = cur.next;
                if (l1 != null) l1 = l1.next;
                if (l2 != null) l2 = l2.next;
            }
            if (carry == 1) cur.next = new ListNode(1);
            return dummy.next;
        }
    }

    此解法来自  http://www.cnblogs.com/grandyang/p/4129891.html


    Java解法2:
    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    class Solution {
           public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            ListNode c1 = l1;
            ListNode c2 = l2;
            ListNode sentinel = new ListNode(0);
            ListNode d = sentinel;
            int sum = 0;
            while (c1 != null || c2 != null) {
                sum /= 10;
                if (c1 != null) {
                    sum += c1.val;
                    c1 = c1.next;
                }
                if (c2 != null) {
                    sum += c2.val;
                    c2 = c2.next;
                }
                d.next = new ListNode(sum % 10);
                d = d.next;
            }
            if (sum / 10 == 1)
                d.next = new ListNode(1);
            return sentinel.next;
        }
    }

    此解法来自  https://www.cnblogs.com/wlovet/p/8870325.html



    补充知识:
    1、链表(linked-list):链表就是线性表的链式存储方式。链表的内存是不连续的,前一个元素存储地址的下一个地址中存储的不一定是下一个元素。
      链表通过一个指向下一个元素地址的引用将链表中的元素串起来。 
    2、结点:单向链表有时候也分为有头结点和无头结点。有头结点的链表实现比较方便(每次插入新元素的时候,不需要每次判断第一个节点是否为空),
    并且可以直接在头结点的数据块部分存储链表的长度,而不用每次都遍历整个链表
    3、双向链表: 
    双向链表就是有两个方向的链表。同单向链表不同,在双向链表中每一个节点不仅存储指向下一个节点的指针,而且存储指向前一个节点的指针。
    通过这种方式,能够通过在O(1)时间内通过目的节点直接找到前驱节点,但是同时会增加大量的指针存储空间。 




  • 相关阅读:
    博客园随笔备份Java脚本
    vue 获取 referer
    EntityFramework的天坑
    清空stringbuilder
    相关的验证的正则表达式
    清空StringBuilder的三种方法及效率
    oracle中的字符串函数详解
    浅谈C# application.DoEvent作用
    C# 简单Tcp通信demo
    C#中http请求下载的常用方式demo
  • 原文地址:https://www.cnblogs.com/yswyzh/p/10677211.html
Copyright © 2011-2022 走看看