Skip to content

Instantly share code, notes, and snippets.

@danielrobertson
Created July 7, 2016 19:43
Show Gist options
  • Save danielrobertson/146c43d2597531dd6501b43d11572196 to your computer and use it in GitHub Desktop.
Save danielrobertson/146c43d2597531dd6501b43d11572196 to your computer and use it in GitHub Desktop.
Reverse a singly linked list
Node reverse(Node n) {
// maintain previous node, rewriting all pointers to previous
Node prev = null;
while(n != null) {
Node next = n.next;
n.next = prev;
prev = n;
n = next;
}
return prev;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment