Reversing a Linked List: Iterative and Recursive Approaches in C and Java

How to Reverse a Linked List in C and Java: Iterative and Recursive Approaches

Linked lists are a fundamental data structure in computer science, used for efficient data storage and manipulation. This article will explore two methods to reverse a singly linked list: an iterative approach and a recursive approach. Both methods can be implemented in C or Java. Let's dive in!

Linked List Basics

A linked list is a linear data structure in which each element is a separate object. These objects are called nodes, and each node is composed of two parts: data and a reference (or link) to the next node.

Iterative Approach in C

The iterative method involves traversing the list while maintaining pointers to the current node, previous node, and the next node. As we move through the list, we update the links to reverse the list.

Example Code in C

class Node {
    int data;
    Node *next;
    public Node(int data) {
          data;
          NULL;
    }
}
Node* reverseLinkedList(Node *head) {
    Node *prev  NULL;
    Node *current  head;
    while (current ! NULL) {
        Node *next_node  current-next;
        current-next  prev;
        prev  current;
        current  next_node;
    }
    return prev;
}

This function starts with the head of the linked list and iteratively sets each node's next pointer to the previous node. The traversal continues until the original head becomes the new tail of the reversed linked list.

Recursive Approach in Java

A recursive approach involves breaking the problem down into smaller subproblems. We start by reversing the sublist following the current node, then add the current node to the reversed sublist.

Example Code in Java

public class Solution {
    public ListNode reverseList(ListNode head) {
        if (head  null ||   null) {
            return head;
        }
        ListNode node  reverseList();
          head;
          null;
        return node;
    }
}

Here, the function reverseList is called recursively until the base case (i.e., the last node) is reached. Once the base case is hit, the function works its way back up, reversing the list by adjusting the next pointers.

Choosing Between Iterative and Recursive

Time Complexity: Both iterative and recursive methods have a time complexity of O(n).

Space Complexity: The iterative approach has a space complexity of O(1) since it does not use any additional data structures other than the pointers. The recursive approach has a space complexity of O(n), as each recursive call adds a new frame to the call stack.

For scenarios where memory usage is a concern, the iterative approach is preferable. For cases where the code clarity is more important, the recursive approach might be easier to understand and maintain.

Conclusion

Reversing a linked list is a common problem in computer science. By understanding the iterative and recursive approaches, you can effectively manipulate linked lists in C or Java. Experiment with both methods to find the one that best suits your needs.