Last active
September 2, 2021 21:50
-
-
Save giovannipds/ca5040c82b43d6823aab3f39e0e9d35c to your computer and use it in GitHub Desktop.
Data structures / linked lists JavaScript challenge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
# Challenge | |
Given the follow linked lists: | |
Input: l1 = [2,4,3], l2 = [5,6,4] | |
Do: 342 + 465 = 807 | |
And output another linked list: [7,0,8] | |
-- | |
Other example: | |
Input: l1 = 8,7,5, l2 = 9,0,5 | |
Do: 578 + 509 = 1087 | |
Output: 7801 | |
-- | |
Consider a linked list an object with value and next, value and next etc. until needed. | |
Do no write the inputs as arrays. | |
Consider writing the possible tests you would have done, no need to implement the test, just describe them. | |
# Solution | |
Write the solution below: | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
# Challenge | |
Given the follow linked lists: | |
Input: l1 = [2,4,3], l2 = [5,6,4] | |
Do: 342 + 465 = 807 | |
And output another linked list: [7,0,8] | |
-- | |
Other example: | |
Input: l1 = 8,7,5, l2 = 9,0,5 | |
Do: 578 + 509 = 1087 | |
Output: 7801 | |
-- | |
Consider a linked list an object with value and next, value and next etc. until needed. | |
Do no write the inputs as arrays. | |
Consider writing the possible tests you would have done, no need to implement the test, just describe them. | |
# Solution | |
Write the solution below: | |
*/ | |
// 'the inputs should be a linked list' | |
// 'the input has next and value attributes' | |
// 'next must be an object' | |
// 'next must have the property value'' | |
// 'value must be a number' | |
// 'it can have an optional next' | |
// 'output must reverse the number' | |
// 'first index should be the last index of the input' | |
// 'last index should be the first index of input' | |
// 'the method returns the first linked list with the right sorting' | |
const firstLinkedList = { | |
value: 2, | |
next: { | |
value: 4, | |
next: { | |
value: 3 | |
} | |
} | |
} | |
const secondLinkedList = { | |
value: 5, | |
next: { | |
value: 6, | |
next: { | |
value: 4 | |
} | |
} | |
} | |
function sumLinkedLists(l1, l2) { | |
console.log(prepareLinkedList(l1)) | |
console.log(prepareLinkedList(l2)) | |
return prepareLinkedList(l1) + prepareLinkedList(l2); | |
} | |
function prepareLinkedList(l) { | |
var sum = [] | |
let keepRunning = true; | |
let currentValue = l; | |
while(keepRunning) { | |
sum.push(currentValue.value); | |
if(currentValue.next) { | |
currentValue = currentValue.next; | |
} else { | |
keepRunning = false; | |
} | |
} | |
return sum.reverse(); | |
} | |
console.log(sumLinkedLists(firstLinkedList, secondLinkedList)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment