Good morning! Here's our prompt for today.
We're provided the following two linked lists:
1 -> 2 -> 3 -> 4
and 2 -> 5 -> 8
Each one represents a number in reversed order, so 1 -> 2 -> 3 -> 4
represents 4321
and 2 -> 5 -> 8
represents 852
(note: for an extra challenge, consider if they weren't reversed).
The lists are guaranteed to have at least one node and will not have any leading 0
s. Each of the nodes contain a single digit.
Can you write a method to add the two numbers and return it as another linked list?

JAVASCRIPT
1// list1: 1 -> 2 -> 3 ->4
2// list2: 2 -> 5 -> 8
3
4addLLNums(list1, list2);
5// should return 3 -> 7 -> 1 -> 5
6// 4321 + 852 = 5173

Constraints
- Length of both linked lists
list1
,list2
<=100000
- Value stored in each node is between
0
and9
- Make sure that you have to return a linked list and not a number
- Expected time complexity :
O(n)
- Expected space complexity :
O(n)
Try to solve this here or in Interactive Mode.
How do I practice this challenge?
xxxxxxxxxx
74
'PASSED: Adding the numbers in `list1` and `list2` should result in 4 -> 6 -> 8 -> 0 -> 3 -> 5 -> 7 -> 8 -> 1'
var assert = require('assert');
​
/*
* @param {LinkedListNode} list1
* @param {LinkedListNode} list2
* @return {LinkedListNode}
*/
​
function addLLNums(list1, list2) {
// add list1 and list2
return result;
}
​
function LinkedListNode(val) {
this.val = val;
this.next = null;
}
​
var list1 = new LinkedListNode(3);
var nodes1 = [4, 5, 6, 7, 8, 9, 9];
createNodes(list1, nodes1);
​
var list2 = new LinkedListNode(1);
var nodes2 = [2, 3, 4, 5, 6, 7, 8];
createNodes(list2, nodes2);
​
var list3 = new LinkedListNode(1);
var nodes3 = [2, 3, 4];
createNodes(list3, nodes3);
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment
Here's how we would solve this problem...
How do I use this guide?