GithubHelp home page GithubHelp logo

Comments (1)

liaobin312716 avatar liaobin312716 commented on July 28, 2024

我的思路:

本质上是按位做相加,满十进一。
要遍历做加法,先将链表转换成数组。

所以整体分为3个步骤:

  1. 把链表转换为数组,方法为判断节点的next是否为空,不为空则push到数组。
  2. 遍历数组,按位相加,如果满十,则用临时变量 up 存储,下一位计算时加上 up 值。
  3. 遍历数组,创建节点,组装成链表。

具体实现代码如下:

/**
 * 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) {
    
    function node2arr(node) {
        var arr = [node.val];
        while(node.next) {
            node = node.next;
            arr.push(node.val);
        }
        return arr;
    }
    
    function add(arr1, arr2) {
        var result = [];
        var temp = 0;
        var up = 0;
        
        var diffLen = Math.abs(arr1.length - arr2.length);
        var diffArr = [];
        for (var j=0; j< diffLen; j++) {
            diffArr.push(0);
        }
        
        if (arr1.length < arr2.length) {
            arr1 = diffArr.concat(arr1);
        } else {
            arr2 = diffArr.concat(arr2);
        }

        for (var i=arr1.length-1;i >=0 ;i--) {
            temp = arr1[i] + arr2[i] + up;
            if (temp > 9) {
                result.push(temp % 10);
                up = 1;
            } else {
                result.push(temp);
                up = 0;
            }
        }
        if (up) {
            result.push(up);
        }
        return result;
    }
    
    function arr2Node(arr) {
        var node = new ListNode(arr[0]);
        var temp = node;
        for (var i=1;i< arr.length; i++) {
            temp.next = new ListNode(arr[i]);
            temp = temp.next;
        }
        return node;
    }
    
    var l1Arr = node2arr(l1).reverse();
    var l2Arr = node2arr(l2).reverse();
    var l3Arr = add(l1Arr, l2Arr);
    
    return arr2Node(l3Arr);

from dailyleetcode.

Related Issues (3)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.