Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function reverseRecursive(obj) {
- return obj;
- }
- function reverseIterative(obj) {
- return obj;
- }
- /***************************/
- /***************************/
- /* 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([]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement