Skip to content

Instantly share code, notes, and snippets.

@CooperAtive
Last active August 29, 2015 13:57
Show Gist options
  • Save CooperAtive/9544469 to your computer and use it in GitHub Desktop.
Save CooperAtive/9544469 to your computer and use it in GitHub Desktop.
List Reversal
var list =
{ head: 1,
tail: { head: 2,
tail: { head: 3,
tail: { head: 4,
tail: { head: 5,
tail: null
}
}
}
}
};
function listReverse(acc, list){
if (list === null) return acc;
return listReverse({head: list.head, tail: acc}, list.tail);
}
console.log(JSON.stringify(listReverse(null, list)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment