Skip to content

Instantly share code, notes, and snippets.

@gabefinch
Last active October 27, 2020 22:43
Show Gist options
  • Save gabefinch/de8283bc358c7b0a9896c378797461c0 to your computer and use it in GitHub Desktop.
Save gabefinch/de8283bc358c7b0a9896c378797461c0 to your computer and use it in GitHub Desktop.
Linked list reverser
function reverseList(input, accumulator) {
var reversed = {
id: input.id,
next: accumulator || null
};
return input.next
? reverseList(input.next, reversed)
: reversed;
}
var list = {
id: 'one',
next: {
id: 'two',
next: {
id: 'three',
next: null
}
}
};
reverseList(list);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment