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
  • 相关阅读:
    【python系列】安装完PyCharm创建项目提示No Python interpreter selected
    银联支付测试
    【linux系列】linux防火墙的关闭开启
    【linux系列】vmware12pro安装centos7
    【linux系列】安装虚拟机时候的3中网络模式
    JavaBean与Map<String,Object>相互转换
    【CSS系列】布局篇
    【CSS系列】对表单和数据表格应用样式
    【CSS系列】图像映射
    【Linux系列】find命令使用
  • 原文地址:https://www.cnblogs.com/theskulls/p/4757389.html
Copyright © 2011-2022 走看看