Last week I posted my second contest with a coding problem to solve. Now it's time to acknowledge and reward the winners! <center>  </center> # Contest #2 In a **Linked list**, each _node_ has a value and a next node. Given a Linked list: 1) Write a function that reverse the linked list, using a **recursive** algorithm. 2) Write a function that reverse the linked list, using a **iterative** algorithm. - For instance: given A -> B -> C it returns C -> B ->A You don't have to clone the nodes, you must just work on their _next_ properties. The preferred language to use this time is _javascript_, but you can also use other languages as well. You will need to _translate_ the starting code to the language of your choice. ## Winners There were 5 participants and all of them solved the problems well! Perhaps the problem was easier than I thought! I’ll try and make the next ones harder! As promised, the rewards will be based on clarity of the code and the time of submission. Hope to see your solutions to my future contests too! ## Best solution ``` function reverseRecursive(obj) { var next = obj && obj.next; if(!next){ return obj; } var reversedTail = reverseRecursive(next); obj.next = null; next.next = obj; return reversedTail; } function reverseIterative(obj) { var reversed; while(obj){ var next = obj.next; obj.next = reversed; reversed = obj; obj = next; } return reversed; } /***************************/ /***************************/ /* DO NOT CHANGE DOWN HERE */ /***************************/ /***************************/ function Obj(v, next) { this.v = v; this.next = next; } Obj.prototype.toString = function() { var next = this.next ? ' -> ' + this.next.toString() : ''; return this.v + next; }; Obj.prototype.clone = function() { return new Obj(this.v, this.next && this.next.clone()); }; function arrayToObj(array) { var obj; for (var i = array.length - 1; i >= 0; i--) { obj = new Obj(array[i], obj); } return obj; } function test(array) { var obj = arrayToObj(array); console.log('Original:') console.log(obj && obj.toString()); var r1 = reverseRecursive(obj && obj.clone()); console.log('Reversed (recursive algorithm):'); console.log(r1 && r1.toString()); var r2 = reverseIterative(obj && obj.clone()); console.log('Reversed (iterative algorithm):'); console.log(r2 && r2.toString()); console.log(); } test([1, 2, 3, 4, 5, 6]); test([1]); test([]); ``` ## Rewards The contest post total liquid reward is: 9.409 SBD * 1st price: 50% * @hendrikcrause --- **4.704 SBD** * 2nd price: 25% * @codingdefined --- **2.352 SBD** * 3rd price: 12.5% * @chasmic-cosm --- **1.176 SBD** * 4th price: 6.25% * @pps --- **0.588 SBD** * 5th price: 3.125% * @jjb777 --- **0.294 SBD** ### Thanks for taking part of the challenge! Armando 🐾
author | armandocat |
---|---|
permlink | coding-challenge-2-winners |
category | contest |
json_metadata | {"tags":["contest","coding","programming","challenge","steemstem"],"users":["hendrikcrause","codingdefined","chasmic-cosm","pps","jjb777"],"image":["https://steemitimages.com/DQmQGxeECpik7rVwtMCWndk8cKE4QEemqjRSSKk5NoTn2q8/question-mark-2123968_640.jpg"],"app":"steemit/0.1","format":"markdown"} |
created | 2017-09-22 08:46:39 |
last_update | 2017-09-22 08:46:39 |
depth | 0 |
children | 2 |
last_payout | 2017-09-29 08:46:39 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.194 HBD |
curator_payout_value | 0.030 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 3,009 |
author_reputation | 19,042,686,186,587 |
root_title | "Coding challenge #2 - Winners!" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 15,598,018 |
net_rshares | 79,656,561,328 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
chasmic-cosm | 0 | 5,217,635,314 | 100% | ||
shango | 0 | 11,288,493,259 | 100% | ||
kenchung | 0 | 33,204,759,055 | 100% | ||
flauwy | 0 | 14,372,387,270 | 25% | ||
pps | 0 | 752,819,888 | 100% | ||
codingdefined | 0 | 1,895,678,348 | 100% | ||
hendrikcrause | 0 | 2,525,116,733 | 100% | ||
armandocat | 0 | 10,399,671,461 | 100% |
Wow thanks for the 2nd Prize :)
author | codingdefined |
---|---|
permlink | re-armandocat-coding-challenge-2-winners-20170922t085622848z |
category | contest |
json_metadata | {"tags":["contest"],"app":"steemit/0.1"} |
created | 2017-09-22 08:56:21 |
last_update | 2017-09-22 08:56:21 |
depth | 1 |
children | 0 |
last_payout | 2017-09-29 08:56:21 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 31 |
author_reputation | 534,272,792,694,295 |
root_title | "Coding challenge #2 - Winners!" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 15,598,656 |
net_rshares | 0 |
Thanks @armandocat. For anyone interested in knowing how the code works, here goes: In order to reverse a linked list, one has to loop through the entire list and change the 'next' pointer of each node to point to the previous node. In the case of a recursive function, we "loop" through the list by reversing the current node and the calling the recursive function again on the next node. A recursive function also always need to start with a stop condition to prevent infinite loops and stack-overflows. In this case the stop condition is when the node it's given is either null or has no next node. In the case of an iterative function. We loop through the list with well... a loop. A while loop in this case where we check that the current node we're reversing isn't null. That's it. Simple really... if you have the solution in front of you 😝. Can't wait for the next one.
author | hendrikcrause |
---|---|
permlink | re-armandocat-coding-challenge-2-winners-20170922t101500529z |
category | contest |
json_metadata | {"tags":["contest"],"users":["armandocat"],"app":"steemit/0.1"} |
created | 2017-09-22 10:15:00 |
last_update | 2017-09-22 10:15:00 |
depth | 1 |
children | 0 |
last_payout | 2017-09-29 10:15:00 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.043 HBD |
curator_payout_value | 0.004 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 884 |
author_reputation | 1,510,925,846,074 |
root_title | "Coding challenge #2 - Winners!" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 15,603,806 |
net_rshares | 17,287,083,788 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
chasmic-cosm | 0 | 5,109,497,794 | 100% | ||
codingdefined | 0 | 1,829,584,983 | 100% | ||
armandocat | 0 | 10,348,001,011 | 100% |