Skip to content

Instantly share code, notes, and snippets.

@AutoSponge
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AutoSponge/99d0cdd68da8106380a2 to your computer and use it in GitHub Desktop.
Save AutoSponge/99d0cdd68da8106380a2 to your computer and use it in GitHub Desktop.
reverse a singly-linked list
var d = {id:'d', next:null};
var c = {id:'c', next:d};
var b = {id:'b', next:c};
var a = {id:'a', next:b};
function reverse( list ) {
var head = list.next;
var tail = list;
var next;
tail.next = null;
while ( head ) {
next = head.next;
head.next = tail;
tail = head;
head = next;
}
return tail;
}
reverse( a );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment