# Java implementation using singly linked list - recursion

• ``` public class ReverseLinkedList { public static void main(String[] args) { LinkedNode head = new LinkedNode(0); head = initialize(head); print(head); System.out.println(); LinkedNode stackTop = createStackLinked(head, null); print(stackTop); } private static LinkedNode initialize (LinkedNode head) { LinkedNode firstNode = head; for (int i = 1; i <= 5; i++) { LinkedNode secondNode = new LinkedNode(i); firstNode.next = secondNode; firstNode = secondNode; } return head.next; } private static void print(LinkedNode head) { LinkedNode node = head; while (node != null) { System.out.print(node.val); node = node.next; if (node != null) { System.out.print(" -> "); } } } private static LinkedNode createStackLinked(LinkedNode head, LinkedNode top) { if (head == null) { return top; } LinkedNode node = new LinkedNode(head.val); node.next = top; return createStackLinked(head.next, node); } } class LinkedNode { int val; LinkedNode next; public LinkedNode(int val) { this.val = val; } ```

```} ```

• @bohra but your code is not space O(1)

• @ulyx how can you implement it in O(1)? It will take O(n) space and time complexity.

• @bohra why not code like this?

void printList(Node node) {
if (node != null) {
printList(node.next);
System.out.println(node.val);
}
}

• @ulyx That would still be linear space. Every recursive call will push the current state of your method(in simple words) to a stack internally.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.