Skip to content

Commit

Permalink
Add solution to sum lists for linkedLists that store digits in regula…
Browse files Browse the repository at this point in the history
…r right to left order. Minor updates on other solutions.
  • Loading branch information
harryttd committed May 29, 2017
1 parent bc62f3f commit 8111df4
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 12 deletions.
2 changes: 1 addition & 1 deletion questions-specs/chapter02-linkedLists/2.02-KthToLast.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export function KthToLast2(list, k) {
// O(N) SPACE

function printKthToLast(list, k) {
if (!list) return 0;
if (!list) return 0; // Can Return -1 if input for k is 0
let index = printKthToLast(list.next, k) + 1;
if (index === k) console.log(k + 'th to last node is' + list.value);
return index;
Expand Down
78 changes: 69 additions & 9 deletions questions-specs/chapter02-linkedLists/2.05-sumList.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';
import { arrayToLinkedList, createNode } from './helpers';
import { arrayToLinkedList, createNode, getListLength } from './helpers';

// FIRST SOLUTION
export function sumLists(list1, list2) {
export function sumListsIterative(list1, list2) {
const list1Arr = [], list2Arr = [];

while (list1 || list2) {
Expand All @@ -26,20 +26,80 @@ export function sumLists(list1, list2) {

// |---~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~---|

// RECURSIVE SOLUTION
export function sumLists2(list1, list2, carry = 0) {
// RECURSIVE SOLUTION (Digits stored in reverse order)
export function sumListsRecursive(list1, list2, carry = 0) {
if (!list1 && !list2 && !carry) return null;

const newNode = createNode();
const resultNode = createNode();
let value = carry;

if (list1) value += list1.value;
if (list2) value += list2.value;
newNode.value = value % 10;
resultNode.value = value % 10;

if (list1 || list2) {
const nextNode = sumLists2(list1 ? list1.next : null, list2 ? list2.next : null, value >= 10 ? 1 : 0);
newNode.next = nextNode;
const nextNode = sumListsRecursive(
list1 ? list1.next : null,
list2 ? list2.next : null,
value >= 10 ? 1 : 0
);
resultNode.next = nextNode;
}
return newNode;

return resultNode;
}

// |---~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~---|

// THIS SOLUTION IS FOR LISTS THAT STORE DIGITS IN REGULAR RIGHT TO LEFT ORDER

class PartialSum {
constructor() {
this.nodeSum = null;
this.carry = 0;
}
}

function addLists(list1, list2) {
const len1 = getListLength(list1);
const len2 = getListLength(list2);

if (len1 < len2) list1 = padList(list1, len2 - len1);
else list2 = padList(list2, len1 - len2);

const sum = addListsHelper(list1, list2);

if (!sum.carry) return sum.nodeSum;
else return insertBefore(sum.nodeSum, sum.carry);
}

function addListsHelper(list1, list2) {
if (!list1 && !list2) return new PartialSum();

const sum = addListsHelper(list1.next, list2.next),
value = sum.carry + list1.value + list2.value,
fullResult = insertBefore(sum.nodeSum, value % 10);

sum.nodeSum = fullResult;
sum.carry = Math.floor(value / 10);
return sum;
}

function padList(list, padding) {
for (let i = 0; i < padding; i++) {
list = insertBefore(list, 0);
}
return list;
}

function insertBefore(list, value) {
const node = createNode(value);
if (list) node.next = list;
return node;
}

const list1 = {value: 6, next: {value: 1, next: {value: 7, next: {value: 8, next: null}}}};
const list2 = {value: 2, next: {value: 9, next: {value: 5, next: null}}};
// const list1 = {value: 7, next: {value: 1, next: {value: 6, next: null}}};
// const list2 = {value: 5, next: {value: 9, next: {value: 2, next: null}}};
console.log(addLists(list1, list2));
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { expect } from 'chai';
import * as funcs from './3.05-sortStack';

const numberSort = (a, b) => a < b ? 1 : a > b ? -1 : 0;
// const numberSort = (a, b) => a < b ? 1 : a > b ? -1 : 0;
const numberSort = (a, b) => b - a;

for (let key in funcs) {
let func = funcs[key];
Expand All @@ -27,7 +28,6 @@ for (let key in funcs) {

it(`correctly sorts ${arg}`, function() {
let expected = arg.slice(0).sort(numberSort);

expect(func(arg)).to.eql(expected);
});

Expand Down

0 comments on commit 8111df4

Please sign in to comment.