Input: Given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
Input & Output Illustration (open for understanding)
var addTwoNumbers = function(l1, l2) {
let carry = 0,
sum = 0;
let runningNode = new ListNode(0, null);
let headNode = runningNode;
while (l1 !== null || l2 !== null) {
sum = l1 != null ? l1.val : 0 + l2 != null ? l2.val : 0 + carry;
carry = 0;
// Error is in below line it states "TypeError: Cannot set properties of undefined (setting 'next')"
runningNode.next = ListNode(sum % 10, null)
runningNode = runningNode.next;
//How to fix it?
if (l1) {
l1.next;
}
if (l2) {
l2.next;
}
}
if (carry) {
runningNode.next = ListNode(carry);
}
return headNode;
};