zoukankan      html  css  js  c++  java
  • [leetcode]Add Two Numbers——JS实现

    Javascript的结构体应用,如下:
        function station(name, latitude, longitude){
            this.name = name;
            this.latitude = latitude;
            this.longitude = longitude;
        }
        var s1 = new station('station1', 100, 200);
        console.log(s1.name+" 's latitude :" + s1.latitude );

    /**
     * Definition for singly-linked list.
     * function ListNode(val) {
     *     this.val = val;
     *     this.next = null;
     * }
     */
    /**
     * @param {ListNode} l1
     * @param {ListNode} l2
     * @return {ListNode}
     */
    
    var addTwoNumbers = function(l1, l2) {
        var head = new ListNode(0);
        var temp1 = 0;
        var temp2 = 0;
        var val1;
        var val2;
        while(l1 || l2 || temp1) {
            if (l1)val1 = l1.val;
            else val1 = 0;
            if (l2) val2 = l2.val;
            else val2 = 0;
            temp2 = Math.floor((val1 + val2 + temp1) % 10);
            temp1 = Math.floor((val1 + val2 + temp1) / 10);
            head.val += 1; 
            var newNode = new ListNode(temp2);  
            if(head.next == null){  
                head.next = newNode;  
            }
            else {
                var tempNode = head.next;  
                while(tempNode.next != null)  
                    tempNode = tempNode.next;  
                tempNode.next = newNode;  
            }
            if (l1)l1 = l1.next;
            if (l2)l2 = l2.next;
        }
        return head.next;
    };
  • 相关阅读:
    js如何求一组数中的极值
    五星评分效果 原生js
    省市区三级联动
    jq表头固定
    css垂直居中 两种方法
    node.js grunt文件压缩
    js 定时器
    动态规划---最长公共子序列
    AES,RSA对称加密和非对称加密
    正则表达式学习笔记
  • 原文地址:https://www.cnblogs.com/cheermyang/p/5046777.html
Copyright © 2011-2022 走看看