Skip to content

Instantly share code, notes, and snippets.

@maxlord
Created October 27, 2017 11:47
Show Gist options
  • Save maxlord/c51f95dc18c2ce8fa9225ab2c9b3cd35 to your computer and use it in GitHub Desktop.
Save maxlord/c51f95dc18c2ce8fa9225ab2c9b3cd35 to your computer and use it in GitHub Desktop.
Reversion of Linked Lists
private static Node reverseLinkedListByRecursion(Node node) {
if (node == null || node.next == null) {
return node;
}
Node rest = node.next;
rest = reverseLinkedListByRecursion(rest);
node.next.next = node;
node.next = null;
return rest;
}
private static Node reverseLinkedListByWhile(Node node) {
Node current = node;
Node prev = null;
Node next;
while (current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
return prev;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment