• Javascript
  • Python
  • Go

Reverse Linked List: Java Recursive Approach

The concept of a linked list is a fundamental data structure in computer science. It is a linear data structure where elements are stored in...

The concept of a linked list is a fundamental data structure in computer science. It is a linear data structure where elements are stored in a sequential manner, with each element containing a pointer to the next element. One of the most common operations on a linked list is reversing its order, which can be achieved using various approaches. In this article, we will explore the Java recursive approach for reversing a linked list.

Before we dive into the implementation, let us understand the concept of recursion. Recursion is a programming technique where a function calls itself until a base condition is met. It is a powerful tool in solving complex problems and can be applied to various data structures, including linked lists.

To begin with, let us first define a Node class that will represent each element in our linked list. It will contain two attributes - data, which will store the value of the element, and next, which will point to the next element in the list.

```

class Node {

int data;

Node next;

public Node(int data) {

this.data = data;

this.next = null;

}

}

```

Next, we will create a LinkedList class that will contain the logic for reversing the list. It will have two attributes - head, which will point to the first element of the list, and size, which will keep track of the number of elements in the list.

```

class LinkedList {

Node head;

int size;

public LinkedList() {

this.head = null;

this.size = 0;

}

}

```

Now, let us define the recursive method for reversing the list. The approach is to first reverse the remaining list starting from the second element recursively. Then, we will make the first element point to the second element and the second element point to null, effectively reversing the order.

```

private Node reverse(Node curr, Node prev) {

// base condition - end of list reached

if (curr == null) {

return prev;

}

// recursively reverse the remaining list

Node next = curr.next;

curr.next = prev;

return reverse(next, curr);

}

```

In the above code, we first check if we have reached the end of the list by comparing the current node to null. If yes, we return the previous node, which will become the new head of the list. Otherwise, we recursively call the reverse method, passing the next node as the current node and the current node as the previous node. This continues until we reach the end of the list.

Finally, we need to update the head pointer and the size of the list. This can be done in our LinkedList class as follows:

```

public void reverseList() {

this.head = reverse(this.head, null);

this.size = this.size - 1;

}

```

We subtract 1 from the size as the first element of the list will become the last element after reversing.

To test our implementation, let us create a linked list with the elements 1, 2, 3, 4 and print it before and after reversing.

```

LinkedList list = new LinkedList();

list.head = new Node(1);

list.head.next = new Node(2);

list.head.next.next = new Node(3);

list.head.next.next.next = new Node(4);

System.out.println("Original List:");

list.printList();

list.reverseList();

System.out.println("Reversed List

Related Articles

Java Equivalent of PHP's print_r

Java Equivalent of PHP's print_r When it comes to debugging and troubleshooting code, having a tool that can easily display complex data str...

Utilizing java.math.MathContext

for Accurate Calculations When it comes to numerical calculations, precision and accuracy are of utmost importance. Even the slightest devia...