//Time Complexity: O(N), Space Complexity: O(N).
public static Node recursive(Node head) {
if(head == null || head.next == null) {
return head;
}
Node recursiveNode = recursive(head.next);
head.next.next = head;
head.next = null;
return recursiveNode;
}
//Time Complexity: O(N), Space Complexity: O(1).
public static Node iterative(Node head) {
if(head == null) {
return null;
}
Node previous = head;
Node current = head;
while(head.next != null) {
current = head.next;
head.next = current.next;
current.next = previous;
previous = current;
}
return previous;
}
Reverse LinkedList
最新推荐文章于 2018-12-26 02:45:30 发布