Created
October 27, 2017 11:47
-
-
Save maxlord/c51f95dc18c2ce8fa9225ab2c9b3cd35 to your computer and use it in GitHub Desktop.
Reversion of Linked Lists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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