create account

Coding challenge #2 - Winners! by armandocat

View this thread on: hive.blogpeakd.comecency.com
· @armandocat ·
$0.22
Coding challenge #2 - Winners!
Last week I posted my second contest with a coding problem to solve.
Now it's time to acknowledge and reward the winners!

<center> 
![](https://steemitimages.com/DQmQGxeECpik7rVwtMCWndk8cKE4QEemqjRSSKk5NoTn2q8/question-mark-2123968_640.jpg)
</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 🐾
👍  , , , , , , ,
properties (23)
authorarmandocat
permlinkcoding-challenge-2-winners
categorycontest
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"}
created2017-09-22 08:46:39
last_update2017-09-22 08:46:39
depth0
children2
last_payout2017-09-29 08:46:39
cashout_time1969-12-31 23:59:59
total_payout_value0.194 HBD
curator_payout_value0.030 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length3,009
author_reputation19,042,686,186,587
root_title"Coding challenge #2 - Winners!"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id15,598,018
net_rshares79,656,561,328
author_curate_reward""
vote details (8)
@codingdefined ·
Wow thanks for the 2nd Prize :)
properties (22)
authorcodingdefined
permlinkre-armandocat-coding-challenge-2-winners-20170922t085622848z
categorycontest
json_metadata{"tags":["contest"],"app":"steemit/0.1"}
created2017-09-22 08:56:21
last_update2017-09-22 08:56:21
depth1
children0
last_payout2017-09-29 08:56:21
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length31
author_reputation534,272,792,694,295
root_title"Coding challenge #2 - Winners!"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id15,598,656
net_rshares0
@hendrikcrause ·
$0.05
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.
👍  , ,
properties (23)
authorhendrikcrause
permlinkre-armandocat-coding-challenge-2-winners-20170922t101500529z
categorycontest
json_metadata{"tags":["contest"],"users":["armandocat"],"app":"steemit/0.1"}
created2017-09-22 10:15:00
last_update2017-09-22 10:15:00
depth1
children0
last_payout2017-09-29 10:15:00
cashout_time1969-12-31 23:59:59
total_payout_value0.043 HBD
curator_payout_value0.004 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length884
author_reputation1,510,925,846,074
root_title"Coding challenge #2 - Winners!"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id15,603,806
net_rshares17,287,083,788
author_curate_reward""
vote details (3)