zoukankan      html  css  js  c++  java
  • LeetCode 2-Add Two Numbers

    Add Two Numbers

    You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

    Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
    Output: 7 -> 0 –> 8

    两个数存在两个单链表中,求他们的和,结果还是存在一个链表中。

    Java程序

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            ListNode headNode = new ListNode(0);  
            ListNode currentNode = headNode;  
            
            int carray = 0;  
            while(l1 != null && l2 != null){  
                    carray +=l1.val;
                    l1 = l1.next;
                    
                    carray +=l2.val;
                    l2 = l2.next;
    
               currentNode.next = new ListNode(carray%10);
               currentNode = currentNode.next;
               carray/=10;
               
            }
            if(l1!=null){
               
                    while(l1!=null){
                        carray +=l1.val;
                        l1 = l1.next;
                        currentNode.next = new ListNode(carray%10);
                        currentNode = currentNode.next;
                        carray/=10;
                    }
                }
                
            if(l2!=null){
                    while(l2!=null){
                        carray +=l2.val;
                        l2 = l2.next;
                        currentNode.next = new ListNode(carray%10);
                        currentNode = currentNode.next;
                        carray/=10;
                    }
                }
            if(carray==1){
                currentNode.next = new ListNode(1);
                currentNode = currentNode.next;
            }
           return headNode.next;
        }
    }

    第一两个节点,一个指向头节点,一个指向当前运行节点

    同时遍历两个单链表,对应位置求和,carray%10为新的节点值,carray/10是来进位的

    当有一个链表是空的时候说明是两个长度不同的数相加,对当前节点再考虑进位的情况下,将非空的节点链接起来。

    上面的while是当两个链表都不为空的时候在进行运算,下面再连接上非空的链表

    可把这两个组合在一起

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            ListNode headNode = new ListNode(0);  
            ListNode currentNode = headNode;  
            
            int carray = 0;  
            while(l1 != null || l2 != null){  
                if(l1!=null){
                    carray +=l1.val;
                    l1 = l1.next;
                }
                if(l2!=null){
                    carray +=l2.val;
                    l2 = l2.next;
                }
               currentNode.next = new ListNode(carray%10);
               currentNode = currentNode.next;
               carray/=10;
               
            }
            
            if(carray==1){
                currentNode.next = new ListNode(1);
                currentNode = currentNode.next;
            }
           return headNode.next;
        }
    }

    这样程序简洁多了

    Python程序

    # Definition for singly-linked list.
    # class ListNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution(object):
        def addTwoNumbers(self, l1, l2):
            """
            :type l1: ListNode
            :type l2: ListNode
            :rtype: ListNode
            """
            carry = 0 
            head = ListNode(0)
            currentNode = head
            while l1 or l2:
                if l1:
                    carry +=l1.val
                    l1 = l1.next
                if l2:
                    carry +=l2.val
                    l2 = l2.next
                    
                currentNode.next = ListNode(carry%10)
                currentNode = currentNode.next
                carry = carry//10
                
            if carry==1:
                currentNode.next = ListNode(1)
                currentNode= currentNode.next
            return head.next
  • 相关阅读:
    c#生成图片验证码
    关于Aspcms如何嵌入整个网站,以及网站导航所指向页面的内容显示
    web 验证控件
    MVC Link连接数据库增删改查方法的不同写法
    Mvc 翻页查询,代码很有用
    MVC添加分布视图做唯一验证
    MVc路由查询,路由到底有什么作用呢??
    MVC添加动态视图的参考代码。重点是添加部分视图的使用方法,非常有用的代码!!!!!!!!!!!!!!
    tyvj P1209
    bzoj 1051: [HAOI2006]受欢迎的牛 tarjan缩点
  • 原文地址:https://www.cnblogs.com/bbbblog/p/4757389.html
Copyright © 2011-2022 走看看