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
  • 相关阅读:
    按钮组件如何处理点击事件(将原生事件绑定到自定义组件)
    一个简单的transition动画
    根据路由记录(利用matched)动态生成面包屑导航
    assets和static的异同
    Ceph集群概念以及部署
    腾讯质量效能提升最佳实践:智能自动化测试探索和建设
    腾讯WeTest压测大师通过中国计量科学研究院测试认证,获国家级权威认可
    新办公司每年费用
    2
    Leetcode刷题第三题 无重复字符的最长子串
  • 原文地址:https://www.cnblogs.com/theskulls/p/4757389.html
Copyright © 2011-2022 走看看