0

In this question: https://leetcode.com/problems/add-two-numbers/description/, I've figured out how to get sum of the digits (i.e. 807), but now I need to convert that number into a linked list (e.g. 7 -> 0 -> 8).

How do I create a linkedlist from a number?

Class function for creating a list node:

function ListNode(val) {
   this.val = val;
   this.next = null;
}

Rest of my code:

var addTwoNumbers = function(l1, l2) {

    function findVal(linkedList) {
          return linkedList.next == null ? linkedList.val.toString() : linkedList.val.toString() + findVal(linkedList.next);

    }

    var l1num = parseInt(findVal(l1).split('').reverse().join(''));
    var l2num = parseInt(findVal(l2).split('').reverse().join(''));
    var sum = l1num + l2num;

// Create linked list from sum

2 Answers 2

3

If you turn your number into an array, you can then use the array.prototype.reduce function.

let number = 807;

function ListNode(val) {
  this.val = val;
  this.next = null;
}


// Convert the number into a string and split that into an array of digits
let arr = Array.from(number.toString());

// Iterate through the elements, and create the nodes
let head = arr.reduce((nextNode, curr) => {
  let node = new ListNode(curr);
  node.next = nextNode;
  return node;
}, null);


// Print out the values
let node = head;
while(node) {
  console.log(node.val);
  node = node.next
}

Sign up to request clarification or add additional context in comments.

7 Comments

You really shouldn't need that slice(1). Just pass null as the initial value and use reduceRight.
@user184994, almost. The LL are supposed to be in the opposite order. Starting with the ones, then the tens, hundreds, ... so just change the reduceRight back to a reduce and we should be fine ;)
@Thomas Oh, I didn't notice the OP needs the list backwards.
@Bergi This no longer works following your latest edit- head is undefined. You'll need to call new ListNode as opposed to ListNode
@user184994 Ouch, I didn't mean to remove the new. Thanks for testing :-)
|
1

Easier with recursion:

f = n => n ? { val: n % 10, next: f(n / 10 | 0) } : null

console.log( f(807) )

f = (a, b, c = 0) => a && b && { val: (c += a.val + b.val) % 10, 
                                next: f(a.next, b.next, c > 9) } || null

console.log( f( { val: 2, next: { val: 4, next: { val: 3 } } }, 
                { val: 5, next: { val: 6, next: { val: 4 } } } ) )

11 Comments

what do all the letters represent?
f = n => n is short for function f(n) { return n }, and I used a and b instead of the l1 and l2 parameters. c is just a temporary variable used to get the carry over +1 if the sum of the 2 digits is more than 9. You can see the translated version on typescriptlang.org/play/…
how does n / 10 | 0 work? I just console'd 807 / 10 | 0 and it gave me 80 but no idea how that worked
@cryptofish123 it's one of the shortcuts for converting a floating point number to integer blog.blakesimpson.co.uk/read/…. Sorry about making the code so cryptic .. I was going for "Leet" code :D
Can you explain how c > 9 works? On 3rd iteration c = 10. I assume you want to carry over only 1 to next iteration? How does it do so ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.